2010-09-20 9 views
1

J'ai implémenté un serveur TCP asynchrone généré par un autre processus. Il démarre correctement et fonctionne comme prévu, mais j'ai des problèmes pour mettre fin au serveur lorsque je termine le processus qui l'a démarré.Arrêt d'un serveur TCP asynchrone sur un thread séparé en C#

Ce qui suit est mon serveur TCP actuel et la fonction d'arrêt de l'autre processus.

Serveur TCP

public class StateObject 
    { 
     //Client socket. 
     public Socket workSocket = null; 
     //Size of receive buffer. 
     public const int BufferSize = 1024; 
     //Receive buffer. 
     public byte[] buffer = new byte[BufferSize]; 
     //Received data string. 
     public StringBuilder sb = new StringBuilder(); 
    } 

    public class AsynchronousSocketListener : Strategy 
    { 
     //Thread signal. 
     public static ManualResetEvent allDone = new ManualResetEvent(false); 
     public volatile bool listening = true; 

     //User-specified port number. 
     private int Port; 

     public AsynchronousSocketListener(int port) 
     { 
      Port = port; 
     } 

     public void StopListening() 
     { 
      listening = false; 
     } 

     public void StartListening() 
     { 
      //Data buffer for incoming data. 
      byte[] bytes = new Byte[1024]; 

      //Establish the local endpoint for the socket. 
      IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); 
      IPAddress ipAddress = ipHostInfo.AddressList[0]; 
      IPEndPoint localEndPoint = new IPEndPoint(ipAddress, Port); 

      //Create a TCP/IP socket. 
      Socket listener = new Socket(AddressFamily.InterNetwork, 
       SocketType.Stream, ProtocolType.Tcp); 

      //Bind the socket to the local endpoint and listen for 
      //incoming connections. 
      try 
      { 
       listener.Bind(localEndPoint); 
       listener.Listen(100); 

       while (listening) 
       { 
        //Set the event to nonsignaled state. 
        allDone.Reset();  

        //Start an asychronous socket to listen for connections. 
        Print("Waiting for a connection..."); 
        listener.BeginAccept(
        new AsyncCallback(AcceptCallback), 
         listener); 

        //Wait until a connection is made before continuing. 
        allDone.WaitOne(); 
       } 
      } 
      catch (Exception e) 
      { 
       Print(e.ToString());  
      } 
     } 

     public void AcceptCallback(IAsyncResult arg) 
     { 
      //Signal the main thread to continue. 
      allDone.Set(); 

      //Get the socket that handles the client request. 
      Socket listener = (Socket) arg.AsyncState; 
      Socket handler = listener.EndAccept(arg); 

      //Create the state object. 
      StateObject state = new StateObject(); 
      state.workSocket = handler; 
      handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
       new AsyncCallback(ReadCallback), state); 
     } 

     public void ReadCallback(IAsyncResult arg) 
     { 
      String content = String.Empty; 

      //Retrieve the state object and the handler socket 
      //from the asynchronous state object. 
      StateObject state = (StateObject) arg.AsyncState; 
      Socket handler = state.workSocket; 

      //Read data from the client socket. 
      int bytesRead = handler.EndReceive(arg); 

      if (bytesRead > 0) 
      { 
       //There might be more data, so store the data received so far. 
       state.sb.Append(Encoding.ASCII.GetString(
        state.buffer,0,bytesRead)); 

       //Check for end-of-file tag. If it is not there, read 
       //more data. 
       content = state.sb.ToString(); 
       if (content.IndexOf("<EOF>") > -1) 
       { 
        //All the data has been read from the 
        //client. Display it on the console. 
        Print("Read " + content.Length + " bytes from socket. \n Data : " + content); 
        //Echo the data back to the client. 
        Send(handler, content); 
       } 
       else 
       { 
        //Not all data received. Get more. 
        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
         new AsyncCallback(ReadCallback), state); 
       } 
      } 
     } 

     private void Send(Socket handler, String data) 
     { 
      //Convert the string data to byte data using ASCII encoding. 
      byte[] byteData = Encoding.ASCII.GetBytes(data); 

      //Begin sending the data to the remote device. 
      handler.BeginSend(byteData,0,byteData.Length,0, 
       new AsyncCallback(SendCallback),handler); 
     } 

     private void SendCallback(IAsyncResult arg) 
     { 
      try 
      { 
       //Retrieve the socket from the state object. 
       Socket handler = (Socket) arg.AsyncState; 

       //Complete sending the data to the remote device. 
       int bytesSent = handler.EndSend(arg); 
       Print("Sent " + bytesSent + " bytes to client."); 

       handler.Shutdown(SocketShutdown.Both); 
       handler.Close(); 
      } 
      catch (Exception e) 
      { 
       Print(e.ToString());  
      } 
     } 
    } 

processus de fraie

//private NinjaTerminal.Server server; 
     private NinjaTerminal.AsynchronousSocketListener server; 
     private Thread          listenThread; 
     private int           _Port = 8080; 

    protected override void OnStartUp() 
    { 
     server = new NinjaTerminal.AsynchronousSocketListener(Port); 
     listenThread = new Thread(new ThreadStart(server.StartListening)); 
     listenThread.Start(); 
    } 

    protected override void OnTermination() 
    { 
     listenThread.stopListening(); 
     listenThread.Join(); 
    } 

Maintenant, je l'ai vérifié que OnTermination() est appelé, et il ne se connecte au fil du serveur, mais le thread du serveur ne se termine jamais.

Je voudrais des idées sur pourquoi, et des suggestions pour une meilleure architecture ou plus que bienvenue. A ce stade, je n'ai pas beaucoup investi dans autre chose que la mise en place du serveur TCP, donc si vous avez une idée différente/meilleure, j'aimerais l'entendre.

En outre, j'ai recherché haut et bas à travers StackOverflow pour une réponse déjà et aucun d'entre eux vraiment appliqué à un serveur TCP asynchrone. Et j'utilise .NET 3.5

code plus de la réponse de Reed

stopListening public void() { d'écoute = false; allDone.Set(); }

public void StartListening() 
    { 
     //Data buffer for incoming data. 
     byte[] bytes = new Byte[1024]; 

     //Establish the local endpoint for the socket. 
     IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); 
     IPAddress ipAddress = ipHostInfo.AddressList[0]; 
     IPEndPoint localEndPoint = new IPEndPoint(ipAddress, Port); 

     //Create a TCP/IP socket. 
     Socket listener = new Socket(AddressFamily.InterNetwork, 
      SocketType.Stream, ProtocolType.Tcp); 

     //Bind the socket to the local endpoint and listen for 
     //incoming connections. 
     try 
     { 
      listener.Bind(localEndPoint); 
      listener.Listen(100); 

      while (listening) 
      { 
       //Set the event to nonsignaled state. 
       allDone.Reset();  

       //Start an asychronous socket to listen for connections. 
       Print("Waiting for a connection..."); 
       listener.BeginAccept(
       new AsyncCallback(AcceptCallback), 
        listener); 

       //Wait until a connection is made before continuing. 
       allDone.WaitOne(); 
      } 
      listener.Close(); 
     } 
     catch (Exception e) 
     { 
      Print(e.ToString());  
     } 
    } 

Répondre

5

Vous devez changer StopListening pour inclure également un appel pour signaler votre WaitHandle:

public void StopListening() 
{ 
    this.listening = false; 
    this.allDone.Set(); 
} 

Sans cela, votre routine StartListening se bloque toujours ici:

//Wait until a connection is made before continuing. 
allDone.WaitOne(); 
+0

Ah, maintenant j'avais essayé une variante de ce que vous venez de suggérer ici, mais j'avais utilisé allDone.WaitOne(); dans ma méthode StopListening() plutôt que allDone.Set(); –

+0

Pour ajouter à cette réponse car elle n'est pas complètement terminée, après avoir défini l'écoute de false et la routine StartListening peut recommencer et quitter la boucle while, j'ai encore besoin de fermer l'écouteur. J'ai édité la question avec le code approprié, comme un commentaire n'est pas l'endroit approprié pour ce genre de chose. –

+1

@Zach: J'essayais juste de vous montrer pourquoi le "thread du serveur ne se termine jamais";) –