Detecting Tcp Socket Closed By Partner.


Recommended Posts

Is there anywhere to detect when a socket connection is broken, other than by try to send and detecting the timeout?

I have a "no protocol" device that opens a TCP socket to a server. It holds the socket open permanently so messages can be then be initiated in either direction.

If the Server breaks the connection my Daqfactory system doesn't detect this until it next tries to send a message to the server - then it simply does a .initcomm() and everything works again.

Problem is that if the Server needs to send a message in the meantime it cannot.

The server cannot create the socket as it doesn't know how many client systems there are or, even if it did, whether they are powered on or not (short of constantly polling all known systems).

I don't want to continually do .initcomm(), say once every minute as that could disrupt data coming in if the link is not down. I would be happy to do simple test to see if the link is still open - if there was one - and then .Initcomm() if has been closed.

I do get a Timeout error from the .Read() on that device, but I get that all the time if the server is not sending. I get no different error if the socket has been closed.

Any suggestions?

Rod.

Link to comment
Share on other sites

I don't think that will help as the Ethernet link is not disconnected - it is only the server socket that has closed (server program shut down).

I have established a work-around. When the DAQfactory is in a mode that is not sending data every minute, and therefore not inherrently checking the link is open every minute, I can do a device.initcomm() instead.

That way, when server program re-starts link is re-established and it can send its mesasges to client as required.

Not ideal, but works.

Rod.

Link to comment
Share on other sites

Yes, but if you are pinging the server in a way that it replies then when it goes offline, your query will timeout. Also depending on how the socket closes, its possible just doing a write() will fail in a way you can catch.

Reiniting the port seems a little extreme. Its also important to realize that a failed init can take 45 seconds or more, and if you are constantly reiniting, you can get a situation where you never are in any other mode. Its probably not a problem, but not as clean as simply pinging the socket to find out if its still open.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.