From: Manish on
Hi !
We are facing a problem ,of not able to recognize that a socket has been
closed from the client .But we observed the socket behaviour under this
situation i.e. "1. Conn.EndReceive(result) , is returning number of read byte
=0", "2. when we check socket status ,it is found True", "3. Evenif the
socket is closed by the client (Close Wait State as shown by "netstate" ) if
we call socket send or receive function that don't raise any exception",

Q1. How to recognize that client has closed the socked ?
Our application Code snap is given below for your reference
Q2. When socked has been closed by the client but socke status is tru ,why ?

//*******************************************//
TCPConnInboundAsync c = (TCPConnInboundAsync)result.AsyncState;
try
{

int num_read = Conn.EndReceive(result);
bytesReaded += num_read;
if ((num_read < bytesToRead && bytesToRead != Buffer.Length))
{
currReadingOffset += num_read;
bytesToRead -= num_read;
lastBeginReceive = DateTime.Now;
Conn.BeginReceive(Buffer, currReadingOffset,
bytesToRead, readSocketFlags, ReadComplete, c);
}
else
{
objTimer.Enabled = false;
readCallback.Invoke(result);
}
}
catch(Exception err)
{
objTimer.Enabled = false;
readException = err;
readCallback.Invoke(result);
}
From: Jeroen Mostert on
On 2010-05-27 23:37, Manish wrote:
> Hi !
> We are facing a problem ,of not able to recognize that a socket has been
> closed from the client .But we observed the socket behaviour under this
> situation i.e. "1. Conn.EndReceive(result) , is returning number of read byte
> =0", "2. when we check socket status ,it is found True", "3. Evenif the
> socket is closed by the client (Close Wait State as shown by "netstate" ) if
> we call socket send or receive function that don't raise any exception",
>
> Q1. How to recognize that client has closed the socked ?
> Our application Code snap is given below for your reference
> Q2. When socked has been closed by the client but socke status is tru ,why ?
>
This is by design. If the client closes the connection properly, and you are
receiving, you will see the read operation complete having read 0 bytes. You
will never receive 0 bytes if the socket is still connected. You will only
get an exception if the connection is improperly closed (physical disconnect
or the client slamming the connection shut without teardown). Modify your
code to test for this condition and invoke end-of-connection logic.

--
J.