1

I am getting below error when I am trying to connect to a TCP server. My programs tries to open around 300-400 connections using diffferent threads and this is happening during 250th thread. Each thread uses its own connection to send and receive data.

java.net.SocketException: Connection timed out:could be due to invalid address
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:372)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:233)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:220)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:385)

Here is the code I have that a thread uses to get socket:

socket = new Socket(my_hostName, my_port);

Is there any default limit on number of connections that a TCP server can have at one time? If not how to solve this type of problems?

2
  • what is the logic on your server that handles the connections? are you using a threadpool or just spawning threads wildly? if using a threadpool, you could have run out of threads if you're not closing connections / finishing the request Commented Aug 3, 2010 at 19:17
  • No I am not using thread pool. I start one thread at a time. Each thread opens a new connection. Commented Aug 3, 2010 at 21:24

2 Answers 2

5

You could be getting a connection timeout if the server has a ServerSocket bound to the port you are connecting to, but is not accepting the connection.

If it always happens with the 250th connection, maybe the server is set up to only accept 250 connections. Someone has to disconnect so you can connect. Or you can increase the timeout; instead of creating the socket like that, create the socket with the empty constructor and then use the connect() method:

Socket s = new Socket(); s.connect(new InetSocketAddress(my_hostName, my_port), 90000);

Default connection timeout is 30 seconds; the code above waits 90 seconds to connect, then throws the exception if the connection cannot be established.

You could also set a lower connection timeout and do something else when you catch that exception...

Sign up to request clarification or add additional context in comments.

1 Comment

No. You can't increase the connect timeout beyond the default. You can only decrease it. The above code will time out after the default period if it's less than 90 seconds. The default connection timeout varies between systems but it is of the order of a minute.
0

Why all the connections? Is this a test program? In which case be aware that opening large numbers of connections from a single client stresses the client in ways that aren't exercised by real systems with large numbers of different client hosts, so test results from that kind of client aren't all that valid. You could be running out of client ports, or some other client resource.

If it isn't a test program, same question. Why all the connections? You'd be better off running a connection pool and reusing a much smaller number of connections serially. The network only has so much bandwidth after all; dividing it by 400 isn't very useful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.