TCP 在 idHttpServer(服务器)和 wininet(客户端)上保持活动状态

发布于 2024-11-05 04:38:02 字数 654 浏览 0 评论 0原文

我有一个使用 idHttpServer 开发的网络服务器应用程序。当客户端连接我的网络服务器时,由于某种未知的原因,断开连接(不是正常断开),我的网络服务器不会收到通知。我知道这是正常行为,但我需要知道客户何时死亡。

有几种方法可以做到这一点。我知道两个好方法:

1 - 实现心跳机制。客户端套接字通知服务器它仍然处于活动状态(需要一些工作和一些代码才能使其正常工作)

2 - TCP 保持活动状态。这是我最喜欢的方式,因为它不需要太多的代码和工作。但我对此有一些疑问。

  • 这可以与使用吗 idHttpServer(对于服务器)和 wininet 函数(对于客户端)?
  • 这真的如预期的那样起作用吗?我的意思是,当客户端总是死掉时,服务器会收到通知吗?
  • 有人有在 wininet 上设置这个的示例吗? (我想这必须在客户端设置,对吧?)
  • 有没有更好的方法来通知服务器客户端已死(当然使用 indy 和 wininet)

编辑

我正在使用 Delphi 2010 年和最后一个 Indy10 源代码来自他们的 svn。

I have a webserver application developed using idHttpServer. When a client connects do my webserver and, for some unknown reason, got disconnect (not a gracefully disconnect) my webserver does not get notified. I know this is the normal behavior but I need to know when the client dies.

There are a few ways of doing this. I know 2 good ways:

1 - Implement a heart beat mechanism. The client socket notifies the server that it is still alive (need some work and some code to make it works)

2 - TCP Keep Alive. This is the way I like most because it requires not too much code and work. But I got a few questions regarding this.

  • Is this possible to be used with
    idHttpServer (for the server) and
    wininet functions (for the client)?
  • Does that really works as expected? I mean, the server gets notfied when the client dies all the times?
  • Does anyone have a sample of setting this on wininet? (I guess this must be set on the client side, right?)
  • Is there a better way of notify a server that the client is dead (using indy and wininet, of course)

EDIT

I am using Delphi 2010 and last Indy10 source code from their svn.

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

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

发布评论

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

评论(1

何处潇湘 2024-11-12 04:38:02

如果您使用的是最新的 Indy 10 版本,则可以使用 TIdSocketHandle.SetKeepAliveValues() 方法:

procedure SetKeepAliveValues(const AEnabled: Boolean; const ATimeMS, AInterval: Integer);

例如:

procedure TForm1.IdHTTPServer1Connect(AContext: TIdContext);
begin
  // send a keep-alive every 1 second after
  // 5 seconds of inactivity has been detected
  AContext.Binding.SetKeepAliveValues(True, 5000, 1000);
end;

请注意 ATimeMSAInterval 参数仅在 Win2K 及更高版本上受支持。

否则,请直接使用 TIdSocketHandle.SetSockOpt() 方法手动启用 TCP/IP SO_KEEPALIVE 选项:

procedure TIdSocketHandle.SetSockOpt(ALevel: TIdSocketOptionLevel; AOptName: TIdSocketOption; AOptVal: Integer);

例如:

procedure TForm1.IdHTTPServer1Connect(AContext: TIdContext);
begin
  AContext.Binding.SetSockOpt(Id_SOL_SOCKET, Id_SO_KEEPALIVE, 1);
end;

If you are using an up-to-date Indy 10 release, then you can use the TIdSocketHandle.SetKeepAliveValues() method:

procedure SetKeepAliveValues(const AEnabled: Boolean; const ATimeMS, AInterval: Integer);

For example:

procedure TForm1.IdHTTPServer1Connect(AContext: TIdContext);
begin
  // send a keep-alive every 1 second after
  // 5 seconds of inactivity has been detected
  AContext.Binding.SetKeepAliveValues(True, 5000, 1000);
end;

Note that the ATimeMS and AInterval parameters are only supported on Win2K and later.

Otherwise, use the TIdSocketHandle.SetSockOpt() method directly to enable the TCP/IP SO_KEEPALIVE option manually:

procedure TIdSocketHandle.SetSockOpt(ALevel: TIdSocketOptionLevel; AOptName: TIdSocketOption; AOptVal: Integer);

For example:

procedure TForm1.IdHTTPServer1Connect(AContext: TIdContext);
begin
  AContext.Binding.SetSockOpt(Id_SOL_SOCKET, Id_SO_KEEPALIVE, 1);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文