Detecting Disconnected Socket

  • Thread starter Thread starter James Yang
  • Start date Start date
J

James Yang

Hi,

I am using Sockets to connect to a remote computer and send data using
Socket.Send() and receive using Socket.Receive() (block mode) . for
somereason tho, when the client disconnects the Socket.Receive() just passes
without any exception. Is there any way to detect disconnection using
Sockets?

I believe I saw a way of doing this using IAsyncResult, and eventhandling
but..no article really explained how to use it properly.

BTW. Socket.Connected returns true even tho it was disconnected

thank you in advance

- James
 
Hi

James Yang said:
Hi,

I am using Sockets to connect to a remote computer and send data using
Socket.Send() and receive using Socket.Receive() (block mode) . for
somereason tho, when the client disconnects the Socket.Receive() just passes
without any exception. Is there any way to detect disconnection using
Sockets?

Are you sure that the client really closes the connection? Because if you
simply for example disconnect the network cable, or turn off the power of
the client, your side of the connection will not know about this and will
wait until the connection is restored or reset. I've encountered this many
times especially when connecting to our control devices connected via
TCP/IP. If such a device is reset, you won't know of it until you try to
send another message and receive a connection reset.
I believe I saw a way of doing this using IAsyncResult, and eventhandling
but..no article really explained how to use it properly.

I guess this won't solve your problem, but just google for something like
"asynchronous sockets .NET" and I guess you'll find something.
BTW. Socket.Connected returns true even tho it was disconnected

Well, this seems to be exactly the thing mentioned above...
thank you in advance

- James

Hope this helped,
Stefan
 
James Yang said:
Hi,

I am using Sockets to connect to a remote computer and send data using
Socket.Send() and receive using Socket.Receive() (block mode) . for
somereason tho, when the client disconnects the Socket.Receive() just passes
without any exception. Is there any way to detect disconnection using
Sockets?
James -

If the remote computer gracefully disconnects the session, the
Socket.Receive() method will return with 0 bytes. You must detect that
to know that the remote end has disconnected:

int recv = sock.Receive(data);
if (recv == 0)
//remote client has disconnected
else
//remote client has sent data

Hope this helps solve your problem.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471433012.html
 
If such a device is reset, you won't know of it until you try to
send another message and receive a connection reset.

"...if the remote Socket was shut down gracefully, and all the data was
received, this method immediately returns zero..."

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemnetsocketssocketclassreceivetopic1.htm

You must check for zero as the return value of either Receive or EndReceive.
 
Back
Top