SocketAsyncEventArgs.Completed not starting on Windows 8

When I compile this code on a computer with Windows 7 Ultimate and .NET 4 installed, it works fine, but when I run it using Windows RTM and .NET 4.5, the full event never fires.

class Program { private static Socket _Socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); private static void Main(string[] args) { _Socket.Bind(new IPEndPoint(IPAddress.Any, 5012)); _Socket.Listen(100); var arguments = new SocketAsyncEventArgs(); arguments.Completed += OnAccepted; Accept(arguments); Console.ReadLine(); } private static void Accept(SocketAsyncEventArgs args) { args.AcceptSocket = null; if (!_Socket.AcceptAsync(args)) OnAccepted(null, args); } private static void OnAccepted(object sender, SocketAsyncEventArgs e) { Console.WriteLine("Accepted."); Accept(e); } } 

The interesting thing here is if I set a breakpoint on this line and debug it:

 var arguments = new SocketAsyncEventArgs(); 

And connect this server using Hercules before proceeding, it works like a charm. I do this from the beginning, and then magically, OnAccepted receives a call and writes "Accepted." to the console on every single connection. I use the same code and the same program (Hercules) on a computer with Windows 7 and .NET 4, but it always works.

  • Am I doing something wrong?
  • If not, is this a known bug in my OS or .NET Framework version 4.5?
  • Can anyone reproduce this?

Edit : both operating systems are 64 bits.
Change 2 . I reported this as a bug in Microsoft Connect,.
Change 3 . Find a workaround and send it to Connect (just by creating a fake, first connection).
Change 4 . If someone can reproduce this, join the issue in Connect.
Edit 5 : I saw a question that Thomas mentioned , and I tested whether it called Console.ReadLine or not. It turned out that this is so. If I add Thread.Sleep(3000) before my call to Console.ReadLine and try to connect 3 seconds after starting the program, it works like a charm. Again, it is strange that I need to do this only once before calling Console.ReadLine . If I make one connection before calling Console.ReadLine , then each serial connection works even after calling Console.ReadLine . I mentioned this on the Conect page.
Change 6 . I added a link to another question on the connection page and added another workaround that included calling Thread.Sleep before calling Console.ReadLine , as I mentioned in the edit above.

+4
source share
1 answer

It turned out to be a Windows 8 bug. The best workaround I could find was to start an IOCP operation on another thread.

So, what I need to do in the code example asked in the question is to change this line:

 Accept(arguments); 

To this line in the Main method:

 Task.Run(() => Accept(arguments)).Wait(); 

This prevents Console.ReadLine() from being called to block the IOCP operation.

As a side note: this is just a workaround to an operating system error that will most likely be fixed by updating and, from a reliable point of view, makes this workaround redundant.

This issue has been fixed with the latest version of Windows 8.


Edit: The status of the item that I posted on Connect has been changed to Design. <i> I also received an email that contains the following:

The main problem with this behavior is how IO Termination Ports handled in Windows 8..NET work using termination ports; when their behavior changed, so did .NET. behavior.

Edit 2: The feedback status changes to Active again without any details.

Edit 3: Responses received yet another response from Microsoft:

The latest version of Windows 8 should be fixed. Please note that this is an OS problem and not a .NET problem: you need to make sure that you have the latest version of the OS installed. There were no changes to .NET to cause or fix this problem. "

+1
source

All Articles