TCP通信+Java套接字+ReadTimeoutException



我正在Tandem系统(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

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

谢谢!!!!

  • 它在尝试建立连接时没有引发异常。它将它抛出到某个读取调用中,几乎可以肯定是在ObjectInputStream.的构造函数中。您应该在问题中包含堆栈跟踪
  • 您需要在ObjectInputStream.之前创建ObjectOutputStream
  • 12000毫秒是两分钟,而不是三分钟
  • 您应该在客户端使用readFully(),就像在服务器中一样

相关内容

  • 没有找到相关文章

最新更新