Click to See Complete Forum and Search --> : Detecting a Server Closing Connection


alevine
November 9th, 2006, 03:56 PM
I tried this question on the C# programming forum board a month or so ago with no luck. I figured I give it a shot on this board since is general network related...

I have an embedded server and a C# Client GUI that connects to it via Tcp. Normally the Client initiates closing the connection and all is fine. In some circumstances the server may wish to initiate closing the connection. In this case the server sends the FIN and is ack'd by the client. However the Client (C# GUI) does not follow up with the closing the second half of the pipe. I do not see any events/callbacks I can hook into at the application to detect the server half of the close so the client can follow-up with it's close. Talking to colleagues it seems that they encountered the problem and test the socket by doing a write of 0 bytes and catching the exception (or likewise a read). This seems like a less than optimal solution. Does any one know how to detect the server initiating the close so the aplication can be informed.

It appears that clients are generally expected to close connections not servers. However in general TCP/IP does not speficy such a requirement.

i.e. see:
Internetworking with TCP/IP fourth edition Douglas Comer Figure13.14 Page 240 :)

cjard
November 15th, 2006, 05:54 AM
The underlying TCP is abstracted from the application layers above to the extent that it isnt possible.. I dont think I've ever seen a language where the TCP stack implementation has an event to say that the remote end closed the connection. We usually devolve this to protocol - when emailing the client sends the text "QUIT" and the server says "BYE", drops the connection. The client can drop the connection before the server says "BYE" and the server wont be insulted..

Work the termination of the connection into your protocol and ensure that the client/server can each cope with an abrupt end to the connection

ps; Clients being the closers of connections as a rule is IMHO, false - i would go further to say that its more likely that the closer of the connection be specified in the protocol and its often the server because it has resources to share with others.. it's not fair for a client to tie up the resources so the server usually has the definite say in who ends the call

HTTP - server drops connection after data is delivered (except v1.1)
FTP, SMTP, POP3 - client says bye, server drops connection
others.. i forget

alevine
November 21st, 2006, 06:06 PM
Thanks for the reply.
Not much input on this one (you're the only one).
I implemented a command from the embedded server to PC client requesting the client close the connection, much as you stated. If not closed after xx Seconds Server just drops connection.

Regards,

AAL