protected virtual void OnRaiseClientConnectedEvent(ClientConnectedEventArgs e) { EventHandler <ClientConnectedEventArgs> handler = RaiseClientConnectedEvent; if (handler != null) { handler(this, e); } }
public async void StartListeningForIncomingConnection(IPAddress ipaddr = null, int port = 16011) { if (ipaddr == null) { ipaddr = IPAddress.Any; } if (port <= 0) { port = 16011; } mIP = ipaddr; mPort = port; System.Diagnostics.Debug.WriteLine(string.Format("IP Address: {0} - Port: {1}", mIP.ToString(), mPort)); mTCPListener = new TcpListener(mIP, mPort); try { mTCPListener.Start(); KeepRunning = true; while (KeepRunning) { var returnedByAccept = await mTCPListener.AcceptTcpClientAsync(); mClients.Add(returnedByAccept); Debug.WriteLine( string.Format("Client connected successfully, count: {0} - {1}", mClients.Count, returnedByAccept.Client.RemoteEndPoint) ); TakeCareOfTCPClient(returnedByAccept); ClientConnectedEventArgs eaClientConnected; eaClientConnected = new ClientConnectedEventArgs( returnedByAccept.Client.RemoteEndPoint.ToString() ); OnRaiseClientConnectedEvent(eaClientConnected); } } catch (Exception excp) { System.Diagnostics.Debug.WriteLine(excp.ToString()); } }