问题
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