tags:

views:

45

answers:

1

Hi,

I have a Java program running on an iSeries which connects to a server process on the same host. Here's an edited version of the Java client code:

while (true) {         
 try {
  socket = new Socket(myHost, myPort);
 } catch (UnknownHostException ex) {
  // Quit
 } catch (ConnectException ex) {
  // Wait for a while and retry
  continue;
 } catch (IOException ex) {
  // Quit
 }
 break;
}

// Do stuff with socket...

Occasionally, on calling the Socket() constructor, I get a SocketException which falls into the IOException handler which exits the application (this might be a slightly harsh reaction but I don't want to change this until I understand the issue more fully). Here's the stack trace:

java.net.SocketException: A connection with a remote socket was reset by that  
 at java.lang.Throwable.<init>(Throwable.java:195)                             
 at java.lang.Exception.<init>(Exception.java:41)                              
 at java.io.IOException.<init>(IOException.java:40)                            
 at java.net.SocketException.<init>(SocketException.java:29)                
 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)            
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)              
 at java.net.Socket.connect(Socket.java:488)                                
 at java.net.Socket.connect(Socket.java:438)                                
 at java.net.Socket.<init>(Socket.java:148) 
 at com.foo.AccessObject.<init>(AccessObject.java:36)  

I understand that if I am reading from a socket and the other peer closes it, that I will get a SocketException. I also understand that if the connection fails I will get a ConnectionException. What I don't understand is how I can get a SocketException on creating a Socket. I think I can eliminate dodgy network devices as the connection is going to localhost.

I can potentially work around this issue by catching SocketException instead of ConnectionException but I don't want to mask this issue, at least until I understand it more. Does anyone have any ideas about what could be causing the error?

+2  A: 

A probable reason could be your system gets exhausted of ports (occasionally) which the Socket() creates. Use of connection pool could be helpful. This will help avoid creation of new Socket every time if that is a viable option in your case.

For linux, number of open sockets can be listed by command,

netstat -n.

The limit for number of open files (linux treats sockets as files) can be checked by command,

ulimit

Tushar Tarkas
Thanks, I considered that possibility but discounted it as the code above doesn't execute very frequently, a few hundred times over a 24 hour period. On the other hand, the code above is part of a network server process. It seems that it could be possible for a network server process to exceed the number of available ports. I know from looking up these values on Windows XP that the default maximum ports is approx 4000 (5000 - 1024) and the default timeout is 120 seconds. Does this imply that a server configured in this way cannot handle more than ~ 4000 over a 2 minute period?
PhilDin
For the benefit of anyone following this thread, I have done a little more digging into this. With respect to the question: "Does this imply that a server configured in this way cannot handle more than ~ 4000 over a 2 minute period?", I believe the answer is "yes". One solution to this is to change either the maximum port number or the default timeout or both. On Windows, there are registry settings for both. This can potentially be avoided by having the client reuse socket connections to the server instead of constantly creating and closing sockets.
PhilDin