[Mono-list] Reading from a NonBlocking socket closes the connection

scott@imeem.com scott@imeem.com
Sun, 26 Dec 2004 15:52:30 -0800 (PST)


This something of a nasty discrepancy between .Net and Mono

If you connect using a TCP socket and then set Socket.Blocking = false and
then immediately read with no data on the wire the Receive call throws an
exception - the equivalent of the EAGAIN code. If you catch this exception
and then test the socket's Connected property you'll notice that under
.Net the socket is still Connected  and can be used again, while under
Mono the socket is no longer connected.


IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),80); //
pick a destination which doesn't send data back
Socket sock = new (Socket(endPoint,AddressFamily, SocketType.Stream,
ProtocolType.Tcp);
sock.Connect(endPoint);

if(sock.Connected) {
    try {
        sock.Blocking = false;
        byte[] test = new byte[8];
        sock.Receive(test);
    }
    catch (Exception e)
    {
        Console.WriteLine("Connected = " + sock.Connected);

    }
}

This was observed on Mono 1.1.3

Scott Manley