graceful way for a server to check / close a socket?

sde

Centurion
Joined
Aug 28, 2003
Messages
160
Location
us.ca.fullerton
In a Server/Client TCP connection, what is the most graceful way for the server to release a socket with a TcpListener?

Currently when a client disconnects, an exception is generated.
 
umm, when the remote connection is gracefully closed (using Socket.Shutdown and then Socket.Close), 0 will be returned when performing a Socket.Receive()

some protocols make the server or client send a "QUIT" message and the programs can force close the sockets.
 
thanks HJB, .. hey look, it is your Centurion celebration! =)

ok, so let me get this straight.

- server is listening.
- client app executes Socket.Shutdown() , Socket.Close()
- the server receives a '0'

now, will the server automatically close this socket too? or do i need to program that logic in so it listens for a '0' ?

what is the clean way to close the socket on the server side, same thing?

thanks for your help.
 
Well, when you do Socket.Receive on a blocking socket, the method will not return until data has been received. You get returned an array of bytes. If the method returns an array of bytes with a length of 0, this means that the remote socket was closed and there is no more data to be received.
Sorry about confusing you with the "returns 0".

The server will not automatically close the socket, and you need to "listen" for the 0 length byte array. To cleanly close the socket, just do the same thing ... call shutdown and close.
 
HB, .. just wanted to say thanks a bunch for the help. my server/client is now disconnecting without nasty exceptions!

i really appreciate the help.
 
Back
Top