Page 1 of 1

TCP Socket check if the connection is working

Posted: Fri Jan 27, 2023 7:09 pm
by mikelpapamike
Hello everyone,

I'm building an application in order to control a server via a TCP socket.

I can succesfully open/close the socket to that server on demand. What I'm trying now to achieve, is to check periodically if the connection to server is alive/working and reconnect automatically in case the connection is down. I've tried the "if serverConnection is among the lines of the open sockets" but this reports that the socket is open even if the server is off on the other side.

Also another issue when trying to connect is that if the server is down or unreachable for some reason(Network issue or anything else) when I press the connect button that does: "open socket to serverConnection" if the server is unreachable then my program is unresponsive for some seconds.

Any help would be appreciated.

Re: TCP Socket check if the connection is working

Posted: Mon May 29, 2023 12:28 am
by Emily-Elizabeth
What I've seen in the past is the client sends a ping to the server every 60 seconds or so and if a reply is not received back then the connection is dropped and reconnected. Obviously if the server is offline you won't reconnect.

Re: TCP Socket check if the connection is working

Posted: Mon May 29, 2023 1:36 am
by ghettocottage
What OS is the server running, and also what OS will your client be running on?

Re: TCP Socket check if the connection is working

Posted: Thu Jun 01, 2023 3:31 pm
by mikelpapamike
Both Server and Client will be running on Windows. So I should send a "check" command and wait for the reply to see if the connection is alive? Any possible example maybe?

Kind regards,

Re: TCP Socket check if the connection is working

Posted: Thu Jun 01, 2023 4:07 pm
by SparkOut
Probably easiest to use the shell command just to ping the destination server. Parse the returned information to see if there are replies or failure.
If there are replies then you can establish your socket comms. Otherwise handle the fact of the server being unresponsive accordingly.

Re: TCP Socket check if the connection is working

Posted: Thu Jun 01, 2023 11:56 pm
by mwieder
But do note that the ping command by default pings four times, so remember to use the -n option (-c for any other OS).

Code: Select all

put shell("ping -n1 www.google.com") into tIsConnectedResult

Re: TCP Socket check if the connection is working

Posted: Tue Jun 06, 2023 3:59 pm
by mikelpapamike
Okay, with the ping command I can check if the server is reachable inside the network that the client is connected to. But I want to know when my client is disconnected, not just if the server is reachable, as this way i dont know if my client is connected or disconnected.

Re: TCP Socket check if the connection is working

Posted: Tue Jun 06, 2023 5:13 pm
by ghettocottage
mikelpapamike wrote:
Tue Jun 06, 2023 3:59 pm
Okay, with the ping command I can check if the server is reachable inside the network that the client is connected to. But I want to know when my client is disconnected, not just if the server is reachable, as this way i dont know if my client is connected or disconnected.
Can you write a little command on the server-side that you can send to the server via the socket and it gives a short response? It sounds like you already had this thought:
I should send a "check" command and wait for the reply to see if the connection is alive? Any possible example maybe?
Are you running LC server on the server? If not, do you have PHP or some other server-side thing installed?

Re: TCP Socket check if the connection is working

Posted: Tue Jun 06, 2023 10:43 pm
by mikelpapamike
Every command I send to thee server comes with a reply 200 for successful / 400 for unsuccessful . I can send a generic INFO command as well that replies the server version for example.

Also I would like to mention that, because the client/server usecase is for Broadcasting, the solution has to be checking as frequently as possible(without causing problems though) for the connection between them, as every second is critical.

Re: TCP Socket check if the connection is working

Posted: Wed Jun 07, 2023 12:09 am
by FourthWorld
mikelpapamike wrote:
Tue Jun 06, 2023 3:59 pm
Okay, with the ping command I can check if the server is reachable inside the network that the client is connected to. But I want to know when my client is disconnected, not just if the server is reachable, as this way i dont know if my client is connected or disconnected.
If the server is unreachable, does it matter if the user has connectivity?

Re: TCP Socket check if the connection is working

Posted: Wed Jun 07, 2023 12:17 am
by Emily-Elizabeth
In your protocol, just send a PING from the client and a PONG reply from the server. If they client does not receive the PONG there's something wrong. This is a check you do every minute or two.

Re: TCP Socket check if the connection is working

Posted: Wed Jun 07, 2023 3:11 am
by ghettocottage
mikelpapamike wrote:
Tue Jun 06, 2023 10:43 pm
Every command I send to thee server comes with a reply 200 for successful / 400 for unsuccessful . I can send a generic INFO command as well that replies the server version for example.

Also I would like to mention that, because the client/server usecase is for Broadcasting, the solution has to be checking as frequently as possible(without causing problems though) for the connection between them, as every second is critical.
Just brain-storming: could you write a function that checks for connectivity once (sending your generic INFO command). Then you could run that function first before doing anything involving the server. If there is no connection, it could launch a different function that pauses the app and continuously checks the connection until it is re-established.