TCP connection

Discussion to talk about software related topics only.
Post Reply
SeeCwriter
Posts: 605
Joined: Mon May 12, 2008 10:55 am

TCP connection

Post by SeeCwriter »

Is there a way to determine if a TCP connection is still open before using it? In my case there can a couple of seconds between opening a connection and using the connection. If the other side were to close the connection in the intervening time, how would I know?
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: TCP connection

Post by pbreed »

If the other side has closed either properly or via a reset ....
Any attempt to use (read/write) it would return an error...
Specifically from tcp.h...
TCP_ERR_CLOSING
TCP_ERR_CON_RESET

Depending on how it closed....

A core problem with TCP is natively is has no keep alive... if I device opens a TCP connection then gets physically disconnected, or dies in a fire... the other side will not ever know unless it tries to send data and the acknowledgements and retries all time out...

Best you can do is to use these two functions:

DWORD TcpGetLastRxTime(int fdnet)

Returns the TimeTick value for the last time we received a packet from the other side of the connection....
Note that No traffic means no packets.....thus

You can force the other side of the connection to respond... by sending a keepalive packet.

void TcpSendKeepAlive(int fd);


This is done in a TCP_simple_keepalive example...
Post Reply