Android Question TCP Socket Keepalive?

techknight

Well-Known Member
Licensed User
Longtime User
I am interfacing AsyncStreams and the Socket with an xBee wifi module.

Problem is, after about 20 seconds or so of inactivity the socket closes. xBee probably closing it.

is there a keepalive function? I dont want the socket ever closing unless there is some error, or I hit disconnect.
 

techknight

Well-Known Member
Licensed User
Longtime User
Well if the socket closes. it wont pick back up again unless I completely reinit everything.

how do I ping the socket? Also is there a way I can test if the socket is closed? Logging Socket1.Connected shows that it is true, when clearly the server kicked the socket.
 
Last edited:
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Well if the socket closes. it wont pick back up again unless I completely reinit everything.

how do I ping the socket? Also is there a way I can test if the socket is closed? Logging Socket1.Connected shows that it is true, when clearly the server kicked the socket.

As Erel said, just ping it at a regular interval. Just send a "dummy" packet every x seconds (just write a single character to the AsyncStream). That should stop the connection being dropped at the other end due to inactivity.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Well, I figured that out.

But I just got a Galaxy Tab 4, and I cant open multiple sockets for some reason. Always makes the first connection, then fails with the rest.

and the TCP performance of the first socket is overall slow.

edit: increasing socket timeout to 2500ms helped a bit. A far cry from my consistant socket of 500ms on my Tab 2.
 
Last edited:
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Another issue I ran across and its different across different phones/tablets.

Is this:

Astream.Write(data)
Astream.close
Socket1.Close

This code works. Most of the time. But with different hardware, especially this one, sometimes the Astream.Write doesnt finish, or doesnt get started at all before the Astream.Close event happens, or the socket close. killing off the ability to send the packet.

any ideas how to check for successful transmission before close?
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Are you doing this every time you write to the stream? I generally keep the stream open until I want to close the socket - but that's in a real-time game that's sending messages to & fro pretty much constantly. Dunno - perhaps try a Doevents after the Astream.Write(data) call? Or possibly move the Astream.Close & Socket1.Close to a separate sub & then call it using CallSubDelayed?
 
Upvote 0
Top