#1 2014-07-11 09:59:01

Titou
Member
Registered: 2014-05-11
Posts: 12

TCrtSocket.SockConneted issue

My needs are, binary exchanges (no CR/LF) and possibility to send data at any time (not only in response of something).
Currently I use the Indy components.
Now, I try to use the TCrtSocket to replace the Indy component (For better performances and better mORMot integration).

The test code is :

var
  TCP: TCrtSocket;
  s: RawByteString;
  str: RawByteString;
begin
  TCP := Open('localhost', '9999');
  if TCP<>nil then
  try
    while true do
    begin
      str := 'hello';
      if not TCP.TrySndLow(@str[1], 5) then
      begin
        writeln('Snd error');
        break;
      end;
      Sleep(500);
    end;

    if not TCP.SockConnected then
      writeln('not connected');
  finally
    TCP.Free;
  end;
end.

When I stop the server, TCP.SockConnected continues to return the value true.

How can I test the connection status?

Another issue is the following :
The code below will never stops if I stop the server. The execution loops forever in SockReceiveString.

var
  TCP: TCrtSocket;
  StrReceived: RawByteString;
begin
  TCP := Open('localhost', '9999');
  if TCP<>nil then
  try
    while true do
    begin
      StrReceived := TCP.SockReceiveString();
      writeln(StrReceived);
      Sleep(500);
    end;
  finally
    TCP.Free;
  end;
end.

En fait, j'ai prévu d'utiliser un extrait du code de SockeReceivedString en ajoutant un test avec SockConnected. Mais SockConnected semble ne pas fonctionner.

Offline

#2 2014-07-11 15:19:26

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,666
Website

Re: TCrtSocket.SockConneted issue

SockConnected() calls GetPeerName, so only knows what the client know about the connection.
As stated by MSDN:

If no error occurs, getpeername returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
http://msdn.microsoft.com/en-us/library … p/ms738533

AFAIR, in the IP world, if the server break the connection, it is not notified to the client.

To ensure that we are still really connected with the server, I suppose we need to try to send some data to the server (like a ping/heartbeat command)!

Do you know if there is a safer alternative to check the connection?
We would like it to be fast, also...
:s

Offline

#3 2014-07-15 06:00:05

Titou
Member
Registered: 2014-05-11
Posts: 12

Re: TCrtSocket.SockConneted issue

Yes, of course, you're absolutely right. 
I know now, how I can do. I will use the code of SockReceiveString and will add a timeout.
Thanks for the answer.

Offline

Board footer

Powered by FluxBB