2

I am using SslStream on top of NetworkStream that wraps a Socket, how could i detect Socket disconnection at this situation.

In another words i want to detect if the remote client closed the connection.

Daniel Eugen
  • 2,712
  • 8
  • 33
  • 56

2 Answers2

3

You will get an EOS when reading, or an IOException when writing.

user207421
  • 305,947
  • 44
  • 307
  • 483
2

Use the poll method on the inner socket:

if (client.Client.Poll(0, SelectMode.SelectRead))
{
    read = 0;
    read = br.Read(buffer, 0, buffer.Length);

    if (read > 0)
    {
        Console.Write(Encoding.ASCII.GetString(buffer, 0, read));
    }
    else
        throw new Exception("Socket closed");
}
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Teddy
  • 21
  • 1