Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Socket woes

2 views
Skip to first unread message

David F

unread,
Mar 30, 2005, 4:40:54 PM3/30/05
to
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

unread,
Mar 30, 2005, 5:20:49 PM3/30/05
to

David F wrote:
> 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?

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).

--
Eric....@sun.com

Joseph Dionne

unread,
Mar 31, 2005, 10:37:19 AM3/31/05
to

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

0 new messages