private void StartListening(ref Socket ListeningSocket) { // Note: Do not catch exceptions until we reach the main // listening loop, because <c>StartThread</c> should // intercept initialization exceptions. // Establish the local endpoint for the socket (only on localhost) IPAddress lb = BindAddress ?? (useIPv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback); // Create a TCP/IP socket AddressFamily af = useIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; ListeningSocket = new Socket(af, SocketType.Stream, ProtocolType.Tcp); var endPoint = new IPEndPoint(IPAddress.Loopback, 0); // Bind the socket to the local endpoint and listen for incoming // connections. ListeningSocket.Bind(endPoint); ListeningSocket.Listen(1000); // Notify that the listening thread is up and running IsListening = true; InitListenFinished.Set(); // Main listening loop starts now try { while (IsShuttingDown == false) { listenThreadSwitch.Reset(); if (IsShuttingDown) { break; } ListeningSocket.BeginAccept(AcceptCallback, ListeningSocket); // Wait until a connection is made before continuing listenThreadSwitch.WaitOne(); } } catch (Exception e) {} finally {} }
/// <summary> /// Spawn a thread that listens to incoming connections /// </summary> public void Start(OnNewClient onConnection, Action<Socket> beforeBindListeningSocket) { InitListenException = null; InitListenFinished.Reset(); IsListening = false; IsShuttingDown = false; OnClientStart = onConnection; BeforeBindListeningSocket = beforeBindListeningSocket; ListeningThread = new Thread(StartThread); ListeningThread.Name = "ListenTCP"; ListeningThread.IsBackground = true; ListeningThread.Start(); const int cleanTimeout = 300 * 1000; // in ms CleanTimer = new Timer(new TimerCallback(CheckSockets), null, cleanTimeout, cleanTimeout); }
private void StartThread() { try { StartListening(ref ListeningSocket); } catch (Exception e) { IsListening = false; InitListenException = e; InitListenFinished.Set(); listenThreadSwitch.Set(); } finally { if (ListeningSocket != null) { ListeningSocket.Close(); } } }
void StartThread() { Socket ListeningSocket = null; try { StartListening(ref ListeningSocket); } catch (Exception e) { log.Error(e); IsListening = false; InitListenException = e; InitListenFinished.Set(); ListenThreadSwitch.Set(); } finally { if (ListeningSocket != null) ListeningSocket.Close(); } }
public IPEndPoint Start() { ListeningSocket = null; InitListenException = null; InitListenFinished.Reset(); IsListening = false; IsShuttingDown = false; listeningThread = new Thread(StartThread) { Name = "ListenTCP", IsBackground = true }; listeningThread.Start(); const int cleanTimeout = 300 * 1000; // in ms cleanTimer = new Timer(CheckSockets, null, cleanTimeout, cleanTimeout); InitListenFinished.WaitOne(); EndPoint = (IPEndPoint)ListeningSocket.LocalEndPoint; return(EndPoint); }
/// <summary> /// Open a listener socket and wait for connections /// </summary> void StartListening(ref Socket ListeningSocket) { // Note: Do not catch exceptions until we reach the main // listening loop, because <c>StartThread</c> should // intercept initialization exceptions. // Establish the local endpoint for the socket (only on localhost) IPAddress lb = (BindAddress == null) ? (UseIPv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback) : BindAddress; IPEndPoint localEndPoint = new IPEndPoint(lb, this.LocalPort); // Create a TCP/IP socket AddressFamily af = UseIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; ListeningSocket = new Socket(af, SocketType.Stream, ProtocolType.Tcp); log.Info("Listening to incoming IPv" + (UseIPv6 ? "6" : "4") + " connections on port " + LocalPort); if (BeforeBindListeningSocket != null) BeforeBindListeningSocket(ListeningSocket); // Bind the socket to the local endpoint and listen for incoming // connections. ListeningSocket.Bind(localEndPoint); ListeningSocket.Listen(1000); // Notify that the listening thread is up and running IsListening = true; InitListenFinished.Set(); // Main listening loop starts now try { while (!IsShuttingDown) { #if DEBUG_ACCEPT_CONNECTION log.Debug("Reset signal"); #endif ListenThreadSwitch.Reset(); if (IsShuttingDown) break; #if DEBUG_ACCEPT_CONNECTION log.Debug("BeginAccept (before)"); #endif ListeningSocket.BeginAccept( new AsyncCallback(AcceptCallback), ListeningSocket); #if DEBUG_ACCEPT_CONNECTION log.Debug("Wait signal"); #endif // Wait until a connection is made before continuing ListenThreadSwitch.WaitOne(); #if DEBUG_ACCEPT_CONNECTION log.Debug("Received signal"); #endif } } catch (Exception e) { log.Error(e); } finally { log.Debug("Stopped listening on port " + LocalPort); } }