TCP Socket check if the connection is working

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
mikelpapamike
Posts: 32
Joined: Sat Feb 27, 2021 12:17 am

TCP Socket check if the connection is working

Post by mikelpapamike » Fri Jan 27, 2023 7:09 pm

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.

Emily-Elizabeth
Posts: 101
Joined: Mon Jan 03, 2022 7:10 pm

Re: TCP Socket check if the connection is working

Post by Emily-Elizabeth » Mon May 29, 2023 12:28 am

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.

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: TCP Socket check if the connection is working

Post by ghettocottage » Mon May 29, 2023 1:36 am

What OS is the server running, and also what OS will your client be running on?

mikelpapamike
Posts: 32
Joined: Sat Feb 27, 2021 12:17 am

Re: TCP Socket check if the connection is working

Post by mikelpapamike » Thu Jun 01, 2023 3:31 pm

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,

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: TCP Socket check if the connection is working

Post by SparkOut » Thu Jun 01, 2023 4:07 pm

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.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: TCP Socket check if the connection is working

Post by mwieder » Thu Jun 01, 2023 11:56 pm

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

mikelpapamike
Posts: 32
Joined: Sat Feb 27, 2021 12:17 am

Re: TCP Socket check if the connection is working

Post by mikelpapamike » 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.

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: TCP Socket check if the connection is working

Post by ghettocottage » Tue Jun 06, 2023 5:13 pm

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?

mikelpapamike
Posts: 32
Joined: Sat Feb 27, 2021 12:17 am

Re: TCP Socket check if the connection is working

Post by mikelpapamike » 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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: TCP Socket check if the connection is working

Post by FourthWorld » Wed Jun 07, 2023 12:09 am

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?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Emily-Elizabeth
Posts: 101
Joined: Mon Jan 03, 2022 7:10 pm

Re: TCP Socket check if the connection is working

Post by Emily-Elizabeth » Wed Jun 07, 2023 12:17 am

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.

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: TCP Socket check if the connection is working

Post by ghettocottage » Wed Jun 07, 2023 3:11 am

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.

Post Reply

Return to “Internet”