1

We have a Java client server application with a custom protocol using TCP/IP. We have found it necessary to use a heartbeat within the protocol due to dead socket connection issues.

We have had the heartbeat since the beginning going from client to server with the server responding with an acknowledgment.

We have recently had a timeout issues with the clients, and after analysing the code have come up with a couple of questions I am unsure about.

1 - What direction is best for a heartbeat, I think we chose 'client to server' as it takes the load of the server. I was thinking of changing it to 'server to client' however we have control of both the client and server code, so we don't need worry so much about time wasting clients.

2 - Is it necessary to acknowledge heartbeats to prove the connection is alive in both directions?

Many thanks

4

1 回答 1

0

我认为任何一个方向的流量都应该足以让它保持活力,但用“ping”和“pong”响应并没有什么坏处。传统上,客户端发送心跳,服务器将负责关闭无响应的客户端,因此您所拥有的听起来是正确的。

您是否尝试将超时设置为零?可能是网络设备干扰了您的套接字连接超时吗?

try {
  ServerSocket server = new ServerSocket(2048);
  server.setSoTimeout(0); // never time out
  try {
    Socket s = server.accept(  );
    // handle the connection
    // ...
  }
  catch (InterruptedIOException e) {
    System.err.println("No connection within 30 seconds");
  }
  finally {
    server.close(  );
  }
catch (IOException e) {
  System.err.println("Unexpected IOException: " + e);
}
于 2011-08-11T22:41:10.073 回答