Howdy -
I have a client program that throws the following exception:
Address already in use: connect
java.net.BindException: Address already in use: connect
This client rapidly creates socket connections in a loop for stress
testing the server. The server works great! :) I didn't expect
the client to break tho. The last test I did, the client looped
about 4200 times on the first run, 810 times when run a few seconds
later, then about 4200 times after waiting a few minutes.
I suspect it's a garbage collection problem. Could my program be
consuming sockets faster than the garbage collector cleans them
up?
Any insight would be greatly appreciated.
[Sorry, not a working example. But it is an accurate skeleton]
Socket connection;
for (int i=0; i < 5000; i++)
{
try
{
connection = new Socket(server,port);
connection.setSoTimeout(15000);
out = connection.getOutputStream();
in = connection.getInputStream();
byte[] request = new byte[1000];
byte[] reply = new byte[1000];
int length;
// fill request here
out.write(request);
length = in.read(reply);
// process reply here
// fill request here
out.write(request);
length = in.read(reply);
// process reply here
in.close();
out.close();
connection.close();
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
System.out.println("Looped "+i);
System.exit(1);
}
Eric Sosman - 30 Mar 2005 23:20 GMT
> Howdy -
>
[quoted text clipped - 11 lines]
> consuming sockets faster than the garbage collector cleans them
> up?
It seems more likely that you're consuming port numbers
faster than TCP/IP can recycle them. After a socket is
closed the port number remains unavailable for a time (four
minutes "by statute," IIRC, although it's fairly common for
Web servers to use shorter intervals); this is to allow time
for stale packets to expire from the network. (You wouldn't
want a packet that had been temporarily trapped in a routing
loop to escape and disrupt a new unrelated connection that
happened to use the same port number ...)
See java.net.Socket#setReuseAddress(boolean).

Signature
Eric.Sosman@sun.com
Joseph Dionne - 31 Mar 2005 16:37 GMT
> Howdy -
>
[quoted text clipped - 55 lines]
> System.exit(1);
> }
I assume you are running on Windows. Windows default socket pool limit.
This can be increased via a registry entry change,
http://www.microsoft.com/technet/itsolutions/network/deploy/depovg/tcpip2k.mspx#EDAA
Joseph