[Mono-list] "Operation on non-blocking socket would block" exception with TcpClient

Colin JN Breame colin at breame.net
Fri Mar 17 09:38:44 EST 2006


Hello again,

If I set the ReceiveTimeout on a TcpClient and then try and read from it's 
stream when there is no data available, an exception is thrown [1].

See [2] for the test program.  To test, compile and run then connect. e.g.
 $ netcat localhost 8080
An exception is thrown immediately.

Do I have to make the underlying socket "blocking"? or is this a bug?

 -- Colin

[1] Exception:
Unhandled Exception: System.IO.IOException: Read failure ---> 
System.Net.Sockets.SocketException: Operation on non-blocking socket would 
block
in <0x0006d> System.Net.Sockets.Socket:Receive_nochecks (System.Byte[] buf, 
Int32 offset, Int32 size, SocketFlags flags)
in <0x00064> System.Net.Sockets.Socket:Receive (System.Byte[] buf, Int32 
offset, Int32 size, SocketFlags flags)
in <0x00075> System.Net.Sockets.NetworkStream:Read (System.Byte[] buffer, 
Int32 offset, Int32 size)--- End of inner exception stack trace ---

in <0x000c3> System.Net.Sockets.NetworkStream:Read (System.Byte[] buffer, 
Int32 offset, Int32 size)
in <0x00028> System.IO.Stream:ReadByte ()
in <0x000b4> test_t:Main ()

[2] Test program:
using System.IO;
using System.Net;
using System.Net.Sockets;
public class test_t {
  public static void Main() {
    IPEndPoint endpoint =
      new IPEndPoint(Dns.GetHostByName("localhost").AddressList[0], 8080);
    TcpListener listener = new TcpListener(endpoint);
    listener.Start();
    TcpClient client = listener.AcceptTcpClient();
    client.ReceiveTimeout = 10;
    Stream stream = client.GetStream();
    stream.ReadByte();
    client.Close();
  }
}


More information about the Mono-list mailing list