Exemplo n.º 1
0
 /// <summary>
 /// Allow data to be read from clients
 /// </summary>
 public override void StartListening()
 {
     try
     {
         serverClosed = false;
         TcpListener serverSocket = new TcpListener(IPAddress.Any, m_port);
         TcpClient   clientSocket = default(TcpClient);
         serverSocket.Start();
         new Thread(() =>
         {
             try
             {
                 for (int i = 0; i < MaximumIncomingConnections && !serverClosed; i++)
                 {
                     clientSocket = serverSocket.AcceptTcpClient();
                     ThreadPool.QueueUserWorkItem(ServerThread, new ServerStartInfo(null, clientSocket));
                     connectedClients.Add(clientSocket);
                 }
             }
             catch (Exception ex)
             {
                 OnListenStop?.Invoke(this, new ListenStopEventArgs("An error occures while trying to accept incoming data", ex));
             }
         }).Start();
         OnListenStart?.Invoke(this, ListenStartEventArgs.Empty);
     }
     catch (SocketException ex)
     {
         throw new Exception("Already listening. If you wish to listen on another port instantiate another object", ex);
     }
 }
Exemplo n.º 2
0
 public override void StopListening(string reason)
 {
     serverClosed = true;
     OnListenStop?.Invoke(this, new ListenStopEventArgs(reason));
 }
Exemplo n.º 3
0
 public override void StopListening()
 {
     serverClosed = true;
     OnListenStop?.Invoke(this, new ListenStopEventArgs("Canceled by user"));
 }