我想尽快接受传入的 TCP 连接。然而,我还想为每个接收数据的连接提供一个额外的线程。
这是我监听端口的 TThread 类:
procedure TListenThread.Execute;
var
iSize : Integer;
begin
ConnectionAttempts := 0;
while not (terminated) do begin
iSize := SizeOf(cAddr);
hClient := Accept(hServer, @cAddr, @iSize);
if (hClient <> INVALID_SOCKET) then begin
inc (ConnectionAttempts);
SynchIP := inet_ntoa(cAddr.sin_addr);
Synchronize(WriteToLog); // Processes very fast!
with TReceiveThread.Create(TRUE) do begin // This takes the longest...
FreeOnTerminate := TRUE;
hSocket := hClient;
TheForm := aForm;
Host := SynchIP;
Resume;
end;
end;
end;
end;
我看到 APICreateThread
需要相当长的时间来处理。有没有办法更快地接受连接(所以accept
比 ListenerThread 具有更高的优先级)?
例如accept
,在 2 秒内具有最高优先级(在 2 秒内服务器接受了大约 200 个连接),然后立即创建(200)个线程,或类似的东西。建议,帮助将不胜感激。
PS.:我不想在连接发生之前创建任何线程。(这将限制连接并填充内存)。我也想远离 Indy - 我已经测试过它,它似乎是相同的速度。