2015-12-23 172 views
-1

我有兩個應用程序,一個是tcpclient,另一個是tcp服務器。我將字符串作爲數據從tcpclient發送到tcp服務器。所有工作正常的情況下,單個客戶端,但當我嘗試從另一臺機器運行另一個客戶端時,我得到異常「連接強制cloased遠程主機」請幫我在這我應該如何連接多個客戶端單個tcp服務器這是我的代碼。如何在C#中將多個客戶端連接到單個服務器

/*TCP SERVER*/ 
    static void Main(string[] args) 
     { 
      //192.168.0.105 
      IPAddress ipAd = IPAddress.Parse("192.168.0.100"); 
     // use local m/c IP address, and 
     // use the same in the client 
      FileStream f = File.Open("D:/GPSResearchandDevelopment/GPSService/GPSService/abc.txt", FileMode.OpenOrCreate); 
      IAsyncResult a = null; 
      connection: 
      Console.WriteLine("reCHED TO THE CONNECTION"); 

      TcpListener myList = new TcpListener(ipAd, 8001); 


      myList.Start(); 


      Console.WriteLine("The server is running at port 8001..."); 
      Console.WriteLine("The local End point is :" + 
           myList.LocalEndpoint); 
      Console.WriteLine("Waiting for a connection....."); 

      Socket s = myList.AcceptSocket(); 

      Console.WriteLine("Connection accepted from " + s.RemoteEndPoint); 

      byte[] b = new byte[100]; 
      int k = s.Receive(b); 

      Console.WriteLine("Recieved..."); 

      a = f.BeginWrite(b, 0, b.Length, null, null); 
      f.EndWrite(a); 

      String[] str = Encoding.UTF8.GetString(b).Split('|'); 
      for (int i = 0; i < str.Length; i++) 
       Console.WriteLine(str[i]); 

      ASCIIEncoding asen = new ASCIIEncoding(); 
      s.Send(asen.GetBytes("The string was recieved by the server.")); 
      Console.WriteLine("\nSent Acknowledgement"); 

      //s.Close(); 
      myList.Stop(); 

      goto connection; 


     } 
/*TCP CLIENT*/ 

static void Main(string[] args) 
     { 
     CONNECTAGAIN: 
      TcpClient tcpclnt = new TcpClient(); 

      Console.WriteLine("Connecting....."); 


      tcpclnt.Connect("192.168.0.100", 8001); 
      // use the ipaddress as in th1e server program 

      Console.WriteLine("Connected"); 
      Console.Write("Enter the string to be transmitted : "); 

      String str = Console.ReadLine(); 
      //String str = "supriya|laxman|medankar"; 
      Stream stm = tcpclnt.GetStream(); 

      ASCIIEncoding asen = new ASCIIEncoding(); 
      byte[] ba = asen.GetBytes(str); 
      Console.WriteLine("Transmitting....."); 

      stm.Write(ba, 0, ba.Length); 

      byte[] bb = new byte[100]; 
      int k = stm.Read(bb, 0, 100); 

      for (int i = 0; i < k; i++) 
      { 
       Console.Write(Convert.ToChar(bb[i])); 


      } 

      tcpclnt.Close(); 
//   Console.ReadLine(); 
      goto CONNECTAGAIN; 
     } 
+0

你想在同一時間多個連接?在這種情況下,你需要一些並行運行的線程來接受連接嘗試。 –

+0

總之,您需要爲每個客戶端創建並維護一個套接字和線程。您編寫的代碼一次只允許一個連接。接收調用應該在單獨的線程上進行,因爲它是一個阻塞調用。 – alan7678

+0

是的,我想同時連接多個 –

回答

0

基本上大部分的主線程應該做的是

create a server socket //TcpListener mylist 

while(true) 
    listen for new connection and create a client socket //Socket s 
    spawn thread to handle client socket connection 

你應該封裝產生的線程中的一類,因此您可以在插座作爲存儲爲方便和清理一員。

您spawn /創建的線程應該運行與客戶端交互的邏輯。

即。所有這些代碼。

Console.WriteLine("Connection accepted from " + s.RemoteEndPoint); 

      byte[] b = new byte[100]; 
      int k = s.Receive(b); 

      Console.WriteLine("Recieved..."); 

      a = f.BeginWrite(b, 0, b.Length, null, null); 
      f.EndWrite(a); 

      String[] str = Encoding.UTF8.GetString(b).Split('|'); 
      for (int i = 0; i < str.Length; i++) 
       Console.WriteLine(str[i]); 

      ASCIIEncoding asen = new ASCIIEncoding(); 
      s.Send(asen.GetBytes("The string was recieved by the server.")); 
      Console.WriteLine("\nSent Acknowledgement"); 

      //s.Close(); 
      myList.Stop(); 

如果你這樣做,你可以不斷監聽新的客戶端連接,而不依賴於其他客戶端之間的交互。

相關問題