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.
source share