Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Java Socket Connection Time out

843790Feb 19 2010 — edited Feb 19 2010
Goal*

I want to check host/port availability of computers in our network (~100 computers) .. Just check if host is alive and if it has port 445 opened. I want to do this somehow in parallel, so the code execution is as quick as possible.

My implementation*

I do this by creating socket connection to the host. Something like this:
        Socket s = null;

        try {

            s = new Socket();
            
            InetSocketAddress socketAddress = new InetSocketAddress(ipAddress, 445);

            s.connect(socketAddress, 3000);
            
            if (s.isConnected()) {

                // do something

            } 

        } catch (ConnectException ex) {

            //exception thrown

        } catch (IOException ex1) {

            //exception thrown

        } finally {

            try {
                // close socket
                s.close();
            } catch (IOException ex) {
                Logger.getLogger(ComputerModel.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
This code is executed in a loop, where a separate thread is created for every iteration = ~100 separate threads.

Problem:*

1.
It seems to me, that first ~10-15 connections are created ok, but then my code reaches 'IOException' catch block for every subsequent itteration and ex.getMessage() returns "connection time out" (I try to use different timeout values in the 2nd parameter. Values from 3000 up to 20000).

Is it valid to create 100 separate threads in a loop? Can't I make my NIC to bussy with this ??? What is the maximum ammount of threads, I can create? I did not use threads before, so I have no idea, if max values are near to 10, 100, 1000, or 10000 ..

2.
After my code is executed, I enter 'netstat -an' command in my command line and I get bunch of connections, which report status of SYN_SENT for several connections.

TCP 10.20.11.140:4557 10.30.11.119:445 SYN_SENT
TCP 10.20.11.140:4558 10.30.11.176:445 SYN_SENT
TCP 10.20.11.140:4559 10.30.11.100:445 SYN_SENT
TCP 10.20.11.140:4560 10.30.11.142:445 SYN_SENT
TCP 10.20.11.140:4561 10.30.11.171:445 SYN_SENT
TCP 10.20.11.140:4562 10.30.11.143:445 SYN_SENT
TCP 10.20.11.140:4563 10.30.12.12:445 SYN_SENT
TCP 10.20.11.140:4564 10.30.12.11:445 SYN_SENT
TCP 10.20.11.140:4565 10.30.12.21:445 SYN_SENT
TCP 10.20.11.140:4566 10.30.11.150:445 SYN_SENT

I also feel, that my computer network response (e.g. firefox browsing) is a bit slower ..

How shall I handle this problem properly?

Thx in advance,

Juraj

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 19 2010
Added on Feb 19 2010
5 comments
492 views