Jump to content
Skullcode

how to Properly check if Twsocket Tcp client is still connected ?

Recommended Posts

is this a correct way to check if the Twsocket client is connected ?

 

 

if wsocket1 .State = wsConnected then
//do something 

 

Share this post


Link to post

The only reliable way to know if a TCP socket is still connected is to send something to the other end and receive a response.  If there is no regular traffic, you really don't know the route still exists, so many technical things can cause a connection to fail without any socket close down being received.  If you send something and there is no TCP ACK within several seconds, the data should be resent automatically until finally the socket is closed with an error.  

 

If you are designing a protocol that leave sockets open for long periods, best to send something regularly. 

 

Angus

 

  • Like 1

Share this post


Link to post

Usually you won't even need this check. Check if state is closed and connect if yes; then send data in OnConnect, read in OnDataAvail, close if Recv or Send return -1 or OnError occurs.

Share this post


Link to post

Hello,

I have 45 devices connected to the Socket Server through internet. Internet connection is not stable and my devices connected again.

In the log I found

Client connected. Remote: 10.30.10.1/6146 Local: 10.30.10.199/1001

Client connected. Remote: 10.30.10.1/61949 Local: 10.30.10.199/1001

Client connected. Remote: 10.30.10.1/6666 Local: 10.30.10.199/1001

How I can check active connections? Because in summary I have 128 clients but only 48 is real

Is  code below will be correct?

   for i:=0 to FWSocketServer.ClientCount-1 do

    if (FWSocketServer.Client as TTcpSrvClient).State=wsInvalidState

        then FWSocketServer.Client.CloseDelayed else

        if FWSocketServer.Client.SendStr('test')<0 then

           try

              FWSocketServer.Client.CloseDelayed;

           finally

 

           end;

Regards, Dmytro Lendel

P.S. I can`t login to the forum and can`t reset my password. I do not know why

 

Share this post


Link to post
16 minutes ago, Dmytro Lendel said:

How I can check active connections?

You cannot reliably check for active connection without making communication from client to server or the reverse.

 

When using TWsocket, don't forget that it is asynchronous. You call a method, for example SendStr() and your call return immediately while data is sent in the background. Same for most of other methods. Use the events to execute code when "something" happens. For example when You call SendStr(), you'll get an OnDataSent event when data has been sent for you in the background.

 

Share this post


Link to post

The best solution is for your clients to regularly send something to the server, easy if you write the client code, then you use the last data received tick on TWSocketClient to timeout the connection and close it.  The HTTP server has such a timeout. 

 

Second best is for the server to send data to the client regularly and check there is no error after 45 seconds or something, then close.  If sending data upsets the clients, you can try pinging them which is much faster, but only catches network failure rather then the TCP session being lost.

 

Angus

 

Share this post


Link to post

Hello,

Thank you

My devices have owner ethernet module and I can`t control it. I just can set timeout in it.

After client timeout event, module generate new connection (and does not close old connection manually) to the server. I see 2 connections. Again and again

My application keeps active socket pointer in internal structure. With new connection I can recognize device by ID and update socket pointer with new value. 

Total amount of device is always 48 in the logic list but it can be 457 in FWSocketServer.ClientCount 🙂

As far as I can see, I can use sentstr for all clients and onError event will occurs for invalid clients. Right? Than I`ll need close socket.

 

Regards, 

Dmytro Lendel

Share this post


Link to post
2 minutes ago, Dmytro Lendel said:

As far as I can see, I can use sentstr for all clients and onError event will occurs for invalid clients. Right? Than I`ll need close socket.

Yes, but call Abort not Disconnect.

Share this post


Link to post

You can also check if a new client connection is from the same IP address as an existing client and use that to close the stale connection.  TWSocketClient has a property CPeerAddr you can read.

 

Angus

 

Share this post


Link to post

I am sorry, one more question if you please.

What is different between OnError, OnSocksError, OnBgException events? What I need to use?

Regards,

Dmytro

Share this post


Link to post

In TWSocket, most errors are reported to event handlers, where 0 means no error. 

 

OnSocksError should only relate to proxies, so you don't need that.  onBgException is for errors for which there no event handler involved, background message handling, etc. If  OnError is assigned, it stops an exception being raised for some errors,  like send data failing, so you need to check function return codes carefully.

 

Angus

 

Share this post


Link to post
29 minutes ago, Angus Robertson said:

You can also check if a new client connection is from the same IP address as an existing client

Not reliable: several clients can be behind a single IP. For example when there is a proxy or a modem/router: The server see the public IP address for all clients.

 

Share this post


Link to post
30 minutes ago, Dmytro Lendel said:

What is different between OnError, OnSocksError, OnBgException events? What I need to use?

What Angus said is correct. Personally I never use OnError. I check the error code on all event handler and all method calls, and use OnBgError for the rest.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×