Posts Tagged ‘mono’

A non-blocking socket operation could not be completed

Monday, April 12th, 2010

There is a bug in .NET 2.0 regarding timeout handling by Socket class.
Here’s the code that reproduces this behavior.

using(TcpClient client = new TcpClient())
{
    // Set timeout
    client.ReceiveTimeout =
        (int)TimeSpan.FromSeconds(1).TotalMilliseconds;

    // Connect to perfectly working imap server
    client.Connect("mail.limilabs.com", 143);

    // Create reader and writer
    NetworkStream stream = client.GetStream();
    StreamReader reader = new StreamReader(stream);
    StreamWriter writer = new StreamWriter(stream);

    // Read hello line
    Console.WriteLine(reader.ReadLine());

    // this works with no problems
    writer.WriteLine("a NOOP");             // No operation command
    writer.Flush();
    Console.WriteLine(reader.ReadLine());   // No operation response

    try
    {
        reader.ReadLine();                  // Nothing to read
    }
    catch (Exception ex)                    // Timeout
    {
        Console.WriteLine("timeout");       // This is expected
    }

    writer.WriteLine("b NOOP");             // No operation command
    writer.Flush();

    // SocketException: A non-blocking socket operation
    // could not be completed immediately
    Console.WriteLine(reader.ReadLine());

    client.Close();
}

The above code runs as expected on Mono and on .NET 4.0.

The problem is in UpdateStatusAfterSocketError internal Socket method that executes SetToDisconnected method when any exception is thrown, including timeout exception.

Regex bug on Mono

Sunday, December 6th, 2009

monoThis is a good advice for all creating applications using .NET that need to work on both Microsoft .NET and Mono.

It seems that Mono ignores group indexes specified explicitly.
Here’s the code to reproduce this problem. And the results on Mono are incorrect.

Regex parser = new Regex("(?<2>.*?),(?<1>.*?),");
Match match = parser.Match("first,second,");
Console.WriteLine("Groups[1].Value={0}", match.Groups[1].Value);
Console.WriteLine("Groups[2].Value={0}", match.Groups[2].Value);

MS implementation:

C:1ConsoleApplication1binDebug>ConsoleApplication1.exe
Groups[1].Value=second
Groups[2].Value=first

Mono 2.4.2.3 implementation:

C:1ConsoleApplication1binDebug>mono.exe ConsoleApplication1.exe
Groups[1].Value=first
Groups[2].Value=second

The workaround is simple use names instead of numbers:

Regex parser = new Regex("(?<first>.*?),(?<second>.*?),");
Match match = parser.Match("first,second,");
Console.WriteLine("Groups[first].Value={0}", match.Groups["first"].Value);
Console.WriteLine("Groups[second].Value={0}", match.Groups["second"].Value);