Created
June 25, 2012 22:28
-
-
Save kumpera/2991802 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Threading; | |
| using System.Net; | |
| using System.Net.Sockets; | |
| class Driver { | |
| static bool BrokeConnect; | |
| static void TryConnect (Socket s) { | |
| try { | |
| Console.WriteLine ("--accept from {0}", Thread.CurrentThread.ManagedThreadId); | |
| s.Connect (IPAddress.Loopback, 9999); | |
| } catch (SocketException e) { | |
| BrokeConnect = true; | |
| Console.WriteLine ("from {0} got a {1}", Thread.CurrentThread.ManagedThreadId, e.GetType ()); | |
| } | |
| } | |
| static void AbortConnect () { | |
| Socket listenSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
| IPEndPoint ep = new IPEndPoint(IPAddress.Any, 9999); | |
| listenSocket.Bind (ep); | |
| Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
| Console.WriteLine ("Connecting"); | |
| ThreadPool.QueueUserWorkItem ( delegate { TryConnect (s); }, null); | |
| Thread.Sleep (1000); | |
| s.Close (); | |
| Thread.Sleep (1000); | |
| Console.WriteLine ("Connect broken {0}", BrokeConnect); | |
| } | |
| static bool BrokeAccept; | |
| static void TryAccept (Socket s) { | |
| try { | |
| Console.WriteLine ("--accept from {0}", Thread.CurrentThread.ManagedThreadId); | |
| s.Accept (); | |
| } catch (SocketException e) { | |
| BrokeAccept = true; | |
| Console.WriteLine ("from {0} got a {1}", Thread.CurrentThread.ManagedThreadId, e.GetType ()); | |
| } | |
| } | |
| static void BreakAccept () { | |
| Socket listenSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
| IPEndPoint ep = new IPEndPoint(IPAddress.Any, 9999); | |
| listenSocket.Bind (ep); | |
| listenSocket.Listen (10); | |
| Console.WriteLine ("Accepting"); | |
| ThreadPool.QueueUserWorkItem ( delegate { TryAccept (listenSocket); }, null); | |
| Thread.Sleep (500); | |
| listenSocket.Close (); | |
| Thread.Sleep (500); | |
| Console.WriteLine ("Accept broken {0}", BrokeAccept); | |
| } | |
| static void Main () { | |
| BreakAccept (); | |
| AbortConnect (); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment