【问题标题】:TCP Communication+ Java Socket + ReadTimeoutExceptionTCP 通信 Java Socket + ReadTimeoutException
【发布时间】:2015-05-24 01:42:14
【问题描述】:

我正在串联系统(Unix 环境)中部署我的应用程序。我正在尝试使用 TCP 连接到其他应用程序。我有一个 IP 地址和端口号。从客户端(我的应用程序),我正在写入数据,看起来它正在工作(没有收到任何异常),但是在读取数据时,我收到了 ReadTimeOutException。以下是我的程序,我将感谢您的帮助。有相同的应用程序返回是 C++ 它在相同的 IP 地址和端口号下工作正常

                Socket clientSocket = new Socket();
                clientSocket.setKeepAlive(true);
                clientSocket.setReuseAddress(true);
                clientSocket.setTcpNoDelay(true);
                clientSocket.setSoTimeout(120000); 
                clientSocket.setSendBufferSize(65535);
               clientSocket.connect(new InetSocketAddress("Server IP", "Port Number"), 1000);
                OutputStream outstream = clientSocket.getOutputStream(); 
                outstream .writeInt(msgLen);
                dout.write(msg, 0, msgLen);
                dout.flush();

                DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
                 int len = din.readInt();
                 data = new byte[len];
                 dis .readFully(data);

它在阅读时抛出错误。 尝试建立套接字连接时超时。 java.net.SocketTimeoutException: 读取超时

============================

        Socket clientSocket = new Socket();
        clientSocket.setKeepAlive(true);
        clientSocket.setReuseAddress(true);
        clientSocket.setTcpNoDelay(true);
        clientSocket.setSoTimeout(120000); // Should wait 3 minutes before throwing time out exception - Actually throwing after 2 minutes
        clientSocket.setSendBufferSize(65535);

    InputStream in= new ObjectInputStream(socket.getInputStream());
    OutputStream out = new ObjectOutputStream(socket.getOutputStream());
    out.write(byteArray);
    out.flush();


 // Receive the same string back from the server
    int totalBytesRcvd = 0;  // Total bytes received so far
    int bytesRcvd;           // Bytes received in last read
    while (totalBytesRcvd < byteArray.length) {
      if ((bytesRcvd = in.read(byteArray, totalBytesRcvd,  
                      byteArray.length - totalBytesRcvd)) == -1)
        throw new SocketException("Connection close prematurely");
      totalBytesRcvd += bytesRcvd;
    }


    clientSocket .close();  // Close the socket and its streams

请帮我解决这个问题,从上周开始我就被困在这个问题上。

谢谢!!!!

【问题讨论】:

    标签: sockets tcp inputstream socketexception datainputstream


    【解决方案1】:
    • 它不会在“尝试建立连接时”抛出该异常。它在一些读取调用中抛出它,几乎可以肯定在 ObjectInputStream. 的构造函数中你应该在你的问题中包含堆栈跟踪。
    • 您需要在ObjectInputStream. 之前创建ObjectOutputStream
    • 12000 毫秒是两分钟,而不是三分钟。
    • 您应该在客户端使用readFully(),就像在服务器中一样。

    【讨论】:

    • 感谢您的重播,让我进行更改并相应地尝试。 1)我还发现..另一个运行良好的程序(C++),它使用这个命令来设置IP地址 export TCPIP_PROCESS=\$StakeName 。因此 C++ 程序能够找到 IP 地址并将数据发送到第三方系统。 2)我认为问题是第三方系统,我从我的服务器调用没有访问权限,我必须使用不同的 IP 地址连接才能将数据传递给 TCP IP。像上面有什么办法..我可以在java中进行导出并使用不同的IP地址进行连接。
    • 我的服务器是第三方程序 我不知道使用 TCP 通信的语言。
    猜你喜欢
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    相关资源
    最近更新 更多