本文介绍了如何在TServerSocket中使用SO_KEEPALIVE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
该组件是否具有set选项属性,或者我需要使用setsockopt函数?
Does the component has a set option property or i need to use setsockopt function ?
我想启用Keep-alive内置的操作系统,而不必自己编写... -.-"
i want to enable the os built in Keep-alive instead of me having to write it myself... -.-"
所以,我的问题是,在我创建TServerSocket实例的构造函数内部,然后如何启用此SO_KEEPALIVE选项?
so, my question is, inside the constructor where i create the instance of TServerSocket, how do i then enable this SO_KEEPALIVE option ?
感谢大家.
推荐答案
您可以使用setsockopt设置SO_KEEPALIVE
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;
这篇关于如何在TServerSocket中使用SO_KEEPALIVE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!