0

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.

Community
  • 1
  • 1
Nayan Sonthalia
  • 237
  • 1
  • 4
  • 22
  • Post your client code please, at least the part where you create and connect socket – dezhik Jan 12 '16 at 15:47
  • There is nothing here that sets a read timeout to -1, and -1 isn't a legal read timeout value in the first place. Throw away your `read()` method and use `DataInputStream.readFully().` Don't set the send and receive buffers to such small values. Use the defaults, especially as you don't appear to know wha they are. – user207421 Jan 13 '16 at 15:31
  • Just wanted to check a logical question: settting the send and receive buffer to 2048 at server and 13312 at client will have any problem? – Nayan Sonthalia Feb 09 '16 at 06:34

1 Answers1

1

Looks like we have found the answer to the above issue. I our case the issue was with the bandwidth. The messages were getting lost in the network due to low bandwidth. Its been 14 days since the bandwidth was increased and we have not faced the issue again to date.

Nayan Sonthalia
  • 237
  • 1
  • 4
  • 22