2014-12-08 104 views
0

我有一個簡單的TCP客戶端服務器關係實現。但他們幾乎沒有問題,我不知道如何解決。這裏是客戶端和服務器的代碼: 客戶端和服務器都是分開的。他們中的每一個都是我寫的不同的項目。簡單實現TCP客戶端服務器關係

public void Server() 
    { 
     try 
     { 
      IPAddress IP = IPAddress.Parse("127.0.0.1"); 

      TcpListener Listener = new TcpListener(IP, 8001);    
      Listener.Start(); 

      Socket s = Listener.AcceptSocket(); 

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

      for (int i = 0; i < k; i++){ 
       ConsoleWpfGUI.Write(Convert.ToChar(b[i])); 
      } 

      ASCIIEncoding asen = new ASCIIEncoding(); 
      s.Send(asen.GetBytes("The string:"+strTemp+ " was recieved by the server.")); 

      s.Close(); 
      Listener.Stop(); 

     } 
     catch (Exception e) { 
       ConsoleWpfGUI.WriteLine("Error..... " + e.StackTrace); 
      }  
     } 
    } 

public void Client() 
    { 

     try 
     { 
      TcpClient tcpclnt = new TcpClient(); 

      tcpclnt.Connect("127.0.0.1", 8001); 

      String str = InputString.Text; 
      Stream stm = tcpclnt.GetStream(); 

      ASCIIEncoding asen = new ASCIIEncoding(); 
      byte[] ba = asen.GetBytes(str); 

      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++) 
       ConsoleWpfGUI.Write(Convert.ToChar(bb[i])); 

      tcpclnt.Close(); 
     } 

     catch (Exception e) 
     { 
      ConsoleWpfGUI.Text = "No connection..... "; 
     } 
     } 
    } 

1.聽者不能正常工作,即,收聽捕獲GUI線程(SERV以及我在WPF使用客戶端類)。而不是在單獨的線程中運行,導致應用程序一旦按下「listen」(啓動類服務的Buttoon)就無響應。

  1. 只有在第一個呼叫被回覆的情況下,下一次發送失敗。導致不處理插入並傳遞給服務器端的單獨線程。

我該如何使用線程,serv類不會卡住應用程序。 以及我如何正確使用socketing和傳遞的線程,這將使我的應用程序更多的工作。

謝謝!

+1

最簡單的方法之一 - 使用backgroundworker。 – VikciaR 2014-12-08 08:21:53

回答

3

您需要爲「客戶端」和「服務器」使用單獨的線程。你不能在一個UI線程中運行。因爲監聽會阻止您的UI線程,因此客戶端或服務器都無法正常運行。

您可以通過Internet找到有關Client Server實踐示例的許多資源。只需使用完成任務

好文章線程,背景工人: http://csharp.net-informations.com/communications/csharp-socket-programming.htm http://www.codeproject.com/Tips/607801/SimpleplusChatplusprogramplusinplusChttp://www.codeproject.com/Articles/99143/BackgroundWorker-Class-Sample-for-Beginners

我跑你的代碼。嘗試運行以下兩個控制檯程序 - 分別揉眼睛和運行服務器第一

客戶端 -

  try 
     { 
      TcpClient tcpclnt = new TcpClient(); 

      tcpclnt.Connect("127.0.0.1", 8001); 

      String str = "From CLient"; 
      Stream stm = tcpclnt.GetStream(); 

      ASCIIEncoding asen = new ASCIIEncoding(); 
      byte[] ba = asen.GetBytes(str); 

      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(); 
     } 

     catch (Exception) 
     { 
      Console.WriteLine("No connection..... "); 
     } 

     Console.WriteLine("Transmission end."); 
     Console.ReadKey(); 

服務器 -

 string strTemp = "Hello from server"; 

     try 
     { 
      IPAddress IP = IPAddress.Parse("127.0.0.1"); 

      TcpListener Listener = new TcpListener(IP, 8001); 
      Listener.Start(); 

      Socket s = Listener.AcceptSocket(); 

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

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

      ASCIIEncoding asen = new ASCIIEncoding(); 
      s.Send(asen.GetBytes("The string:" + strTemp + " was recieved by the server.")); 

      s.Close(); 
      Listener.Stop(); 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Error..... " + e.StackTrace); 
     } 

     Console.ReadKey(); 

注 - 只需將其粘貼Main方法裏面試試,你會需要Visual Studio中的Sytem.IO導入()

+0

嘿客戶端和服務器都是分開的。他們中的每一個都是我寫的不同的項目。 – 2014-12-08 09:24:07

+0

是的,如果你喜歡,你可以使用兩個不同的項目,但我建議先去簡單。 編寫兩個控制檯應用程序,一個充當客戶端,另一個充當服務器。兩個通信都使用相同的端口地址由於您有兩個控制檯應用程序,您不需要線程[但是兩個終端可以分別運行它們],只需使用while循環。通過我提供的示例,一旦理解了它們,就可以進行線程化。 – 2014-12-08 09:29:56

+0

這正是我已經做到的。但服務器應用程序窗體在聽時凍結。 – 2014-12-08 09:46:42