Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

Socket woes

Thread view: 
David F - 30 Mar 2005 22:40 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.