I am creating a socket connection, and using a thread to continuously send data through the socket using the following code:
if (FSocketSession.Connected) and (Msg.JSONEvent <> '') then
begin
FSocketSession.Send(TEncoding.UTF8.GetBytes(Msg.JSONEvent))
Msg.Free;
end
else
begin
FSocketSession.Connect;
end;
This is the FSocketSession.Send call:
function TSocketSession.Send(const ADataToSend: TBytes): Integer;
var
Stopwatch: TStopwatch;
SentBytes: Integer;
const
SleepTimeMS = 10;
begin
SiMain.TrackMethod(Self, 'Send');
{$IFDEF USE_SSL}
if FIsSSL then
begin
SiMain.LogInteger('SslState', Ord(FSocket.SslState));
end;
{$ENDIF}
FDataSent := False;
if FSocket.State = wsConnected then
begin
SentBytes := FSocket.Send(@(ADataToSend[Low(ADataToSend)]), Length(ADataToSend));
SiMain.LogInteger('SentBytes', SentBytes);
Stopwatch := TStopwatch.StartNew;
end;
while (FSocket.State = wsConnected) and (not FDataSent) do
begin
FSocket.MessagePump;
Sleep(SleepTimeMS);
if (Stopwatch.ElapsedMilliseconds >= FTimeout) then
begin
FError := CErrorCommTimeout;
break;
end;
end;
Result := FError;
end;
I am noticing that on certain PCs, the socket simply stops sending data after a certain period of time (usually it happens after about 1 or 2 minutes) and on other PCs it runs without any issues. Does anyone see something in the code that could cause this to happen? I can provide more info if needed.