Android Question Socket Closed

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using a socket to send data between a device and my phone.

I have everything working except when the connection is lost.

My phone/app makes the connection to my TCP device and the commands and data is going though fine.
But then on my TCP device I pull the LAN cable out. The connection should now be lost, however the app thinks its still connected. The AStreams_Terminated and AStreams_Error does not get triggered when the connection is lost.

If I then send a command to my TCP device, the app sends the data but does not get anywhere.

The only way I can work out if the conneciton is alive or not is to start a timer when I send the data.
In the timer I am making it count to 5 and if the value gets to 5 before getting a reply from my TCP device then I am closing the connection manually. The AStreams_Terminated sub then activates since the connection is now completly lost.

In the AStreams_Terminated sub I am then trying to connect to my TCP device again.

In VB6.0 you can use the Winsock connection state and use it in a timer to check the state of the connection. However I can't seem to find a way to detect if the connection is still alive or not without polling the TCP device.

I don't mind polling the panel to see if it's there but if the customer is using the app on 3G/4G and the app is running in the background I don't want to keep pinging the TCP device just to check it's status as it will start using the customers data on there plan. (which I don't care, but I am sure the customer will)

The thing is that I want the connection to remain open in the background even when the app is not showing on the screen and as soon as the connection is dropped I want to then connect to the TCP device again and if it fails then I want to then display a notification on the screen telling the customer the connection is lost.

Anyone know a way to detect if the Socket is still open without polling the TCP device and waiting for a reply ?
 

Straker

Active Member
Licensed User
Longtime User
It happens also with connections between PCs. It's always better have sort of req/ack protocol to check over the transmission.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
The only reliable way is to use a timer to check the state. If no reply arrived after x seconds then close the connection.
Yeah that's what I am doing, just didn't know if there was a better way or not.

If I was to send a ASCII command that is 12 ASCII characters long and I got a reply that is 16 ASCII characters long how much data will that be ?
Trying to work out how offen I should poll the TCP device.
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
Yeah that's what I am doing, just didn't know if there was a better way or not.

If I was to send a ASCII command that is 12 ASCII characters long and I got a reply that is 16 ASCII characters long how much data will that be ?
Trying to work out how offen I should poll the TCP device.

I think you should build a little protocol...
With sockets you already have some help. There are automatic functions that rise the NewData event only when all the data is received. But you need the data to be formatted. If you are using text messages you can terminated them with a CRLF and the NewData will rise when the complete message has been received.
If you are transmitting binary data, you can use the astream prefix mode where each transmission has a prefix which contains the data's length and the NewData event rise only when all the data has been received correctly (see astream tutorial for more info).

Or you can build your own protocol.
 
Upvote 0
Top