When I compile this code on a machine with Windows 7 Ultimate and .NET 4 installed, it works just fine but when I try it on one with Windows 8 RTM and .NET 4.5 installed, Complete 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 put a breakpoint at this line and debug it:
var arguments = new SocketAsyncEventArgs();
And connect this server using Hercules before continuing execution, it works like a charm. I do this at the start and then magically, OnAccepted gets called and writes "Accepted." to the console on every single connection. I use the same code and same program (Hercules) on the machine with Windows 7 and .NET 4 but it always works.
- Am I doing something wrong?
- If not, is it a known bug of my OS or .NET Framework version 4.5?
- Can anyone reproduce this?
Edit: Both operating systems are 64 bit.
Edit 2: I reported this as a bug on Microsoft Connect, here.
Edit 3: Found a workaround and post it to Connect (Simply by creating a fake, first connection).
Edit 4: If anyone can reproduce this, please join the issue in Connect.
Edit 5: I saw the question Thomas has mentioned and I tested whether Console.ReadLine
was causing this or not. Turned out it was. If I add Thread.Sleep(3000)
before my Console.ReadLine
call and make a connection attempt in 3 seconds after I run the program, it works like a charm. Again, the odd thing is that I need to do this only once before calling Console.ReadLine
. If I make one connection before calling Console.ReadLine
then every consecutive connection works, even after Console.ReadLine
is called. I'll mention this in the Conect page.
Edit 6: I added the link to the other question to the Connect page and added another workaround that involves calling Thread.Sleep
before calling Console.ReadLine
like I mentioned in the above edit.