Getting “java.net.BindException: Address already in use” when trying to do rapid Socket creation and destruction for load testing?

You are exhausing the space of outbound ports by opening that many outbound sockets within the TIME_WAIT period of two minutes. The first question you should ask yourself is does this represent a realistic load test at all? Is a real client really going to do that?

If not, you just need to revise your testing methodology BTW SO_LINGER is the number of seconds the application will wait during close() for data to be flushed. It is normally zero. The port will hang around for the TIME_WAIT interval anyway if this is the end that issued the close.

This is not the same thing. It is possible to abuse the SO_LINGER option to patch the problem. However that will also cause exceptional behaviour at the peer and again this is not the purpose of a test.

You are exhausing the space of outbound ports by opening that many outbound sockets within the TIME_WAIT period of two minutes. The first question you should ask yourself is does this represent a realistic load test at all? Is a real client really going to do that?

If not, you just need to revise your testing methodology. BTW SO_LINGER is the number of seconds the application will wait during close() for data to be flushed. It is normally zero.

The port will hang around for the TIME_WAIT interval anyway if this is the end that issued the close. This is not the same thing. It is possible to abuse the SO_LINGER option to patch the problem.

However that will also cause exceptional behaviour at the peer and again this is not the purpose of a test.

You are correct in that no real client will be opening that many connections. In my application, real clients will only open 1 TCP connection to the server. What I'm trying to do is simulate real clients and I would like to have as many simulated clients running on the same machine as possible.

– Ones3k2 Jan 17 at 1:54 Your response helped me too! :-) Something I would like to add up here is that if you use the setSoLinger() method, it causes problems for the already connected peers and they sometimes get disconnected too! Don't really know what's causing this behavior but it took me a good 45 mins to figure it out!

– M2X Feb 6 at 16:19 1 @M2X: If you set SO_LINGER to 'false' it causes a connection reset at the peer instead of a proper close when you close the connection. I didn't even want to mention this because it is such an undesirable behaviour. If there are any valid reasons to mess around with SO_LINGER in a normal TCP/IP application I have yet to encounter them ... in over twenty years.

– EJP Feb 11 at 6:13.

I think that you should plan on the port you want to use to connect to be in use. By that I mean try to connect using the given port. If the connect fails (or in your case throws an exception), try to open the connection using the next port number.

Try wrapping the connect statement in a try/catch. Here's some pseudo-code that conveys what I think will work: portNumber = x //where x is the first port number you will try numConnections = 200 // or however many connections you want to open while(numConnections > 0){ try{ connect(host, portNumber) numConnections-- }catch(){} portNumber++ } This code doesn't cover corner cases such as "what happens when all ports are in use?

Use different ports and make sure the ports are not used.

Not using bind() but setReuseAddress(true) is just weird, I hope you do understand the implications of setReuseAddress (and the point of). 100-2000 is not a great number of sockets to open, however the server you are attempting to connect to (since it looks the same addr/port pair), may just drop them w/ a normal backlog of 50. Edit: if you need to open multiple sockets quickly (ermm port scan?), I'd very strongly recommend using NIO and connect()/finishConnect() + Selector.

Opening 1000 sockets in the same thread is just plain slow. Forgot you may need finishConnect() either way in your code.

Or if you are using windows machine then try the following C:\>netstat -aon | findstr 0.0:80 you may get like this TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 560 then do this C:\>tasklist | findstr 560 then kill the process taskkill /F /PID 56.

You are exhausing the space of outbound ports by opening that many outbound sockets within the TIME_WAIT period of two minutes. The first question you should ask yourself is does this represent a realistic load test at all? Is a real client really going to do that?

If not, you just need to revise your testing methodology. BTW SO_LINGER is the number of seconds the application will wait during close() for data to be flushed. It is normally zero.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions