AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Socket woes

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
David F
Guest





PostPosted: Wed Mar 30, 2005 9:40 pm    Post subject: Socket woes Reply with quote



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! Smile 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);
}




Back to top
Eric Sosman
Guest





PostPosted: Wed Mar 30, 2005 10:20 pm    Post subject: Re: Socket woes Reply with quote





David F wrote:
Quote:
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! Smile 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).

--
[email]Eric.Sosman (AT) sun (DOT) com[/email]


Back to top
Joseph Dionne
Guest





PostPosted: Thu Mar 31, 2005 3:37 pm    Post subject: Re: Socket woes Reply with quote



David F wrote:
Quote:
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! Smile 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);
}





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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.