How to use SO_KEEPALIVE with TServerSocket?

故事扮演 提交于 2020-01-25 07:43:26

问题


Does the component has a set option property or i need to use setsockopt function ?

i want to enable the os built in Keep-alive instead of me having to write it myself... -.-"

so, my question is, inside the constructor where i create the instance of TServerSocket, how do i then enable this SO_KEEPALIVE option ?

thanks everyone.


回答1:


You can use setsockopt to set SO_KEEPALIVE

implementation

uses
  WinSock;

{$R *.dfm}

procedure TForm2.ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
var
  OptVal: DWORD;
begin
  OptVal := 1;
  if setsockopt(Socket.SocketHandle, SOL_SOCKET, SO_KEEPALIVE, PAnsiChar(@OptVal), SizeOf(OptVal)) = SOCKET_ERROR then
    raise Exception.Create(Format('WinSock Error %d', [WSAGetLastError()]));
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
 s := TServersocket.Create(Self);
 s.Port := 8090;
 s.OnClientConnect := ClientConnect;
 s.Open;
end;


来源:https://stackoverflow.com/questions/15372281/how-to-use-so-keepalive-with-tserversocket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!