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

791266
100 threads isn't much, it should be ok. The documentation for netstat explains what SYN_SENT is, but I don't know why you have them.
843790
kajbj wrote:
100 threads isn't much, it should be ok.
The documentation for netstat explains what SYN_SENT is, but
I don't know why you have them.
The app has asked the TCP stack to transmit SYN
the TCP stack is now waiting for SYN-ACK.
If there is no host at that IP address or a host that drops connections (SYN packets) for TCP port 445
there will never be a SYN-ACK.
After a few minutes the TCP stack will time out the connection attempt.
791266
tschodt wrote:
kajbj wrote:
100 threads isn't much, it should be ok.
The documentation for netstat explains what SYN_SENT is, but
I don't know why you have them.
The app has asked the TCP stack to transmit SYN
the TCP stack is now waiting for SYN-ACK.
If there is no host at that IP address or a host that drops connections (SYN packets) for TCP port 445
there will never be a SYN-ACK.
After a few minutes the TCP stack will time out the connection attempt.
Yes, I was rather commenting on why his hosts aren't answering. I thought that his case was that all of them should answer :)
843790
kajbj wrote:
tschodt wrote:
kajbj wrote:
The documentation for netstat explains what SYN_SENT is, but
I don't know why you have them.
The app has asked the TCP stack to transmit SYN
the TCP stack is now waiting for SYN-ACK.
If there is no host at that IP address or a host that drops connections (SYN packets) for TCP port 445
there will never be a SYN-ACK.
After a few minutes the TCP stack will time out the connection attempt.
Yes, I was rather commenting on why his hosts aren't answering. I thought that his case was that all of them should answer :)
Akkurat. I was answering more for clarification.
Someone who does not know your posting history could easily assume you meant
it was a mystery why netstat was showing SYN_SENT.
And I figured it would not harm to hint that one possible reason would be that those machines are powered off (no host).
EJP
s.connect(socketAddress, 3000);
If that fails it will throw an exception.
if (s.isConnected()) {
So that test is completely pointless.
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.
If you can catch a connection in this state it is probably about to fail.
1 - 5
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
504 views