Daniel Eugen
Daniel Eugen

Reputation: 2780

Detecting SslStream socket disconnection

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.

Upvotes: 2

Views: 1859

Answers (2)

Teddy
Teddy

Reputation: 21

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");
}

Upvotes: 2

user207421
user207421

Reputation: 310957

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

Upvotes: 3

Related Questions