Check if Socket is connected

ovt001

Member
Licensed User
Longtime User
Hi,
I need to write a apps using socket connection to a server for an hospital.
The nurse need to receive information on a Android PDA.
I need to find a method to check Permanently if the connection to the server is OK.
I was thinking about a timer whoe's check every second if the socket_Connected was = to True.

But maybe exist a better method?

can anybody help?

Thank you
O.
 

ovt001

Member
Licensed User
Longtime User
Hi,
I found a part of the solution.
With AStream lib, I can check if the server side close the connection with the event "terminated".

But how can I configure that when the server side listen again, my socket client will be reconnect automatically?

thank you
O.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
My suggestion is to have a look at Erel's b4a bridge source code.

About your last question. You can have a 'isConnected' flag. When false (for e.g. inside a astreams_terminated sub), you can enable a timer trying to connect to your server. Eventually, when your server is up again, a connection will be established, so you will set your 'isConnected' flag to true.
 
Upvote 0

ovt001

Member
Licensed User
Longtime User
Hi MC73
your idea to use "isConnected" and a timer seems to be a good solution.
but I dont find the flag IsConnected on the Socket or Astream Libs.

Can you tel me where 'isConnected' flag is used?

Thank you
O.
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Check for Socket1.Connected
(or whatever the name of your socket is.)

Rolf
 
Upvote 0

ovt001

Member
Licensed User
Longtime User
Hi
I follow the idea of mc73 and create a boolean 'IsConnected' and a timer to make the reconnection.
When the server application broken the socket connection I receive an Astreams_terminated event. And it works fine.
But if the network goes down (b.e if I plug out the network cable of my server), I don't receive any event....

Can somebody explain this....

Thank you
O.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
When you try to reconnect the socket_connected sub should be triggered and there you should check the 'success' flag of this sub. Doesn't it get triggered? What timeout are you using?
 
Upvote 0

ovt001

Member
Licensed User
Longtime User
Hi mc73,
The problem is that I don't know when I have to reconnect because I don't receive any event who tell me that the connection is broken when a plug out the network cable....

O.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
This is why I asked you to have a look at b4aBridge source. It is a good practice to ping your server every once in a while to check if connectivity is ok. This can be done by a timer. And since you already have a timer, you can ping it from there. You wait for a reply, after pinging, if not arriving in some secs, you can tell that your connections is down :)
 
Upvote 0
Top