public static Thread ToListener(this int port, StreamAction handler) { var t = new Thread( delegate() { var r = new TcpListener(IPAddress.Loopback, port); r.TryStart( delegate { while (true) { // http://stackoverflow.com/questions/365370/proper-way-to-stop-tcplistener var c = r.AcceptTcpClient(); // we wont be able to stop // this loop with current implementation var s = c.GetStream(); new Thread( delegate() { handler(s); } ) { IsBackground = true, }.Start(); } } ); } ) { IsBackground = true, }; t.Start(); return t; }
public static Thread ToListener(this int port, Action<Stream> handler) { var thread = new Thread( delegate() { var r = new TcpListener(IPAddress.Loopback, port); r.TryStart( delegate { r.Start(); while (true) { var c = r.AcceptTcpClient(); var s = c.GetStream(); new Thread( delegate() { handler(s); } ) { IsBackground = true, }.Start(); } } ); } ) { IsBackground = true, }; thread.Start(); return thread; }
public static void ToListener(this int port, StreamAction handler) { new Thread( delegate() { var r = new TcpListener(IPAddress.Loopback, port); r.TryStart( delegate { while (true) { var c = r.AcceptTcpClient(); var s = c.GetStream(); new Thread( delegate() { handler(s); } ) { IsBackground = true, }.Start(); } } ); } ) { IsBackground = true, }.Start(); }
public static ToThreadedTcpListenerInfo ToThreadedTcpListener(this int port, StreamAction handler) { var ret = new ToThreadedTcpListenerInfo(); var t = new Thread( delegate() { var r = new TcpListener(IPAddress.Loopback, port); ret.Listener = r; r.TryStart( delegate { while (true) { // http://stackoverflow.com/questions/365370/proper-way-to-stop-tcplistener // + $exception {"A blocking operation was interrupted by a call to WSACancelBlockingCall"} System.Exception {System.Net.Sockets.SocketException} var c = r.AcceptTcpClient(); // we wont be able to stop // this loop with current implementation var s = c.GetStream(); new Thread( delegate() { handler(s); } ) { IsBackground = true, }.Start(); } } ); } ) { IsBackground = true, }; t.Start(); ret.Thread = t; return ret; }