We have a client server program running which works fine almonst every time. In some weird circumstances it behaves abruptly and causes folloing exception.
At Server
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at com.company.process.SomeClass.read(SomeClass.java:829)
At Client
java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:170) at java.net.SocketInputStream.read(SocketInputStream.java:141) at com.company.package.ClientClass.read(ClientClass.java:479)
I have checked the following link which suggest Connection Reset could happens when on of the participants “violently” closes the connection without using close() and link which suggest the reasion for read timeout counld be a socket taking longer than the timeout value for the data to arrive from the client.
We have checked the values of the socket time out for both serversocket and clientsocket (socket) are set to -1. Can someone please suggest any other reasion that can have caused it?
Note: The application is not a web application. It is just 2 java programms communicating over a socket.
EDIT 1 Snippet of client side code pasted below.
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
public class SocketConnectionClient implements Runnable {
private Socket m_Socket = null;
private InputStream m_inStream = null;
public SocketConnectionClient() {
Connect();
}
public void Connect() {
try {
m_Socket = new Socket("IPAddress", 1234);
m_Socket.setReceiveBufferSize(2048);
m_Socket.setSendBufferSize(2048);
m_Socket.setTcpNoDelay(true);
m_inStream = m_Socket.getInputStream();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// m_Socket.setSoTimeout(m_socketTimeOut);
Thread l_Thread = new Thread(this, "Connect");
l_Thread.start();
}
public void run() {
byte[] bytes = null;
try {
bytes = new byte[4];
read(4, bytes); // exception originate from here.....
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void read(int nbytes, byte[] buffer) throws IOException {
int nrequired, nreceived, nread;
nrequired = nbytes;
nreceived = 0;
byte[] b = buffer == null ? new byte[nbytes] : buffer;
while (nrequired > 0) {
nread = m_inStream.read(b, nreceived, nrequired); // exception is
// thrown
// here......
if (nread == -1) {
// end of stream - ie socket closed
throw new IOException("Bytes read = -1");
} else {
nreceived += nread;
nrequired -= nread;
}
}
}
}
Just as an additional info: We have recently migrated from Windows 2003 server to Windows 2012 server. On the old server we never had this issue but on the new one we get this issue quiet often even when there is absolutely no load on the system. Found this link which says some old plugin can cause this.