心跳方向和确认

发布于 2024-11-28 22:59:14 字数 325 浏览 1 评论 0原文

我们有一个 Java 客户端服务器应用程序,具有使用 TCP/IP 的自定义协议。我们发现由于死套接字连接问题,有必要在协议中使用心跳。

从一开始我们就一直有从客户端到服务器的心跳,服务器会做出确认响应。

我们最近与客户遇到了超时问题,在分析代码后提出了几个我不确定的问题。

1 - 什么方向最适合心跳,我认为我们选择“客户端到服务器”,因为它承担服务器的负载。 我正在考虑将其更改为“服务器到客户端”,但是我们可以控制客户端和服务器代码,因此我们不需要太担心浪费客户端时间。

2 - 是否有必要确认心跳来证明两个方向的连接都处于活动状态?

非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉服软 2024-12-05 22:59:14

我认为任一方向的任何流量都应该足以使其保持活动状态,但用“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);
}

I'm thinking any traffic in either direction should be enough to keep it alive but it doesn't hurt to respond with a "ping" with a "pong". Traditionally the client sends the heartbeat and the server will be responsible to shutting down unresponsive clients so what you have sounds right.

have you tried setting the timeout to zero? Could it be network devices that are interfering with your socket connection timeout?

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文