Hi,
I have a Tcp daemon thread using TSocket from System.Net.Socket (cross platform), listening for incoming TCP connection and creating client threads when clients connect.
It works just fine when compiled and run under Windows. I can connect with a Windows TCP client, a Linux TCP client, or even a simple Linux telnet session, and handle the client communication in the client thread.
When compiled and run under Linux, it seems that TSocket.Accept always returns nil, even when a connection is successfully established via telnet or other client. Hence the code that creates the client threads is never executed....
In Linux, my logs show an infinite series of 'Daemon - Socket.Accept with timeout 500ms' followed by 'Daemon - No new connection after 500ms timeout', although a connection can be successfully established.
However, without a client thread and a reference to the client socket, there isn't much I can do to handle the client communication...
Has anyone seen this behavior when compiling a TCP server for Linux based on System.Net.Socket ?
Any hint or help would be very much appreciated.
procedure TTcpDaemon.Execute;
var
LConnectionSocket: TSocket;
LConnectionThread: TTcpConnectionThread;
begin
FServerSocket := TSocket.Create(TSocketType.TCP, TEncoding.UTF8);
FServerSocket.Listen(FIP, '', FPort);
Log('Dameon started (IP: ' + FIP + ' Port: ' + IntToStr(FPort) + ')');
Log('Daemon - ConnectionThreadCount: ' + IntToStr(GetConnectionThreadCount));
while not Terminated do
begin
try
Log('Daemon - Socket.Accept with timeout 500ms');
LConnectionSocket := FServerSocket.Accept(500);
if Assigned(LConnectionSocket) then
begin
Log('Daemon - New connection - ' + LConnectionSocket.RemoteAddress + ':' + IntToStr(LConnectionSocket.RemotePort));
LConnectionThread := TTcpConnectionThread.Create(LConnectionSocket);
FThreadList.Add(LConnectionThread);
Log('Daemon - Adding new connection thread - ConnectionThreadCount: ' + IntToStr(GetConnectionThreadCount));
Log('Daemon - Starting new connection thread');
LConnectionThread.Start;
end
else
Log('Daemon - No new connection after 500ms timeout');
except
on E: Exception do
Log(Self.ClassName + ' Exception (' + E.ClassName + '): ' + E.Message);
end;
end;
TerminateAllThreads;
FServerSocket.Close(True);
end;