TCP Socket check if the connection is working
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 32
- Joined: Sat Feb 27, 2021 12:17 am
TCP Socket check if the connection is working
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.
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.
-
- Posts: 101
- Joined: Mon Jan 03, 2022 7:10 pm
Re: TCP Socket check if the connection is working
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.
-
- Livecode Opensource Backer
- Posts: 366
- Joined: Tue Apr 10, 2012 9:18 am
Re: TCP Socket check if the connection is working
What OS is the server running, and also what OS will your client be running on?
-
- Posts: 32
- Joined: Sat Feb 27, 2021 12:17 am
Re: TCP Socket check if the connection is working
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,
Kind regards,
Re: TCP Socket check if the connection is working
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.
If there are replies then you can establish your socket comms. Otherwise handle the fact of the server being unresponsive accordingly.
-
- 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
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
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
-
- Posts: 32
- Joined: Sat Feb 27, 2021 12:17 am
Re: TCP Socket check if the connection is working
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.
-
- Livecode Opensource Backer
- Posts: 366
- Joined: Tue Apr 10, 2012 9:18 am
Re: TCP Socket check if the connection is working
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:mikelpapamike wrote: ↑Tue Jun 06, 2023 3:59 pmOkay, 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.
Are you running LC server on the server? If not, do you have PHP or some other server-side thing installed?I should send a "check" command and wait for the reply to see if the connection is alive? Any possible example maybe?
-
- Posts: 32
- Joined: Sat Feb 27, 2021 12:17 am
Re: TCP Socket check if the connection is working
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 9867
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: TCP Socket check if the connection is working
If the server is unreachable, does it matter if the user has connectivity?mikelpapamike wrote: ↑Tue Jun 06, 2023 3:59 pmOkay, 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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Posts: 101
- Joined: Mon Jan 03, 2022 7:10 pm
Re: TCP Socket check if the connection is working
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.
-
- Livecode Opensource Backer
- Posts: 366
- Joined: Tue Apr 10, 2012 9:18 am
Re: TCP Socket check if the connection is working
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.mikelpapamike wrote: ↑Tue Jun 06, 2023 10:43 pmEvery 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.