简体   繁体   English

客户端和服务器中发现java.net.SocketException

[英]java.net.SocketException noticed in client and server

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) java.net.SocketTimeoutException:在java.net.SocketInputStream.socketRead(SocketInputStream.java:116)的java.net.SocketInputStream.socketRead0(本机方法)处读取超时,在java.net.SocketInputStream.read(SocketInputStream.java:170) ),位于com.company.package.ClientClass.read(ClientClass.java:479)的java.net.SocketInputStream.read(SocketInputStream.java:141)

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. 我检查了以下链接 ,该链接建议当参与者中的一个参与者“剧烈地”关闭连接而未使用close()时可能发生连接重置,并且该链接建议读取超时的原因是套接字花费的时间超过数据的超时值从客户那里来。

We have checked the values of the socket time out for both serversocket and clientsocket (socket) are set to -1. 我们已将serversocket和clientsocket(套接字)的套接字超时值都设置为-1。 Can someone please suggest any other reasion that can have caused it? 有人可以建议其他可能导致这种情况的原因吗?

Note: The application is not a web application. 注意:该应用程序不是Web应用程序。 It is just 2 java programms communicating over a socket. 它是通过套接字进行通信的2个Java程序。

EDIT 1 Snippet of client side code pasted below. 编辑1粘贴在下面的客户端代码片段。

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. 作为附加信息:我们最近从Windows 2003服务器迁移到Windows 2012服务器。 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. 找到此链接 ,其中指出某些旧插件可能会导致此问题。

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. 自增加带宽以来已经14天了,迄今为止我们再也没有遇到过这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 服务器/客户端java.net.SocketException - Server/Client java.net.SocketException java.net.SocketException:连接重置服务器客户端 - java.net.SocketException: Connection reset server-client Java客户端-服务器应用程序引发“ java.net.SocketException:连接重置”异常 - Java Client-Server application throws “java.net.SocketException: Connection reset” exception 从dotnet客户端连接到Java服务器时收到“ java.net.SocketException:连接重置” - Receiving “java.net.SocketException: Connection reset” when connecting from dotnet client to a java server java.net.SocketException:尝试将对象从客户端传递到服务器时重置连接 - java.net.SocketException: Connection reset when trying to pass an object from a client to a server java.net.SocketException:连接重置-在客户端和服务器之间以webapp的形式部署在tomcat中 - java.net.SocketException: Connection reset - Between a client and server deployed as webapps in tomcat 运行simpleSSL客户端时出现“ java.net.SocketException:连接重置” - “java.net.SocketException: Connection reset” when running a simpleSSL client java.net.SocketException with fabric8 kubernetes 客户端和观察者 - java.net.SocketException with fabric8 kubernetes client and watcher Netty中的java.net.SocketException - java.net.SocketException in Netty 处理java.net.SocketException - Handling java.net.SocketException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM