2015-06-13 63 views
1

我是套接字編程C#中的新手。我正在嘗試在兩臺計算機之間創建一個聊天服務器,但我無法這樣做,因爲我無法啓動套接字.....我將服務器程序的IP地址給了我,但給了我一個例外......「請求的地址是無效的上下文」 ......這裏是代碼:在我的IP地址打開套接字的例外

 IPAddress hostIPAddress = IPAddress.Parse("178.189.27.85"); 
     TcpListener serverSocket = new TcpListener(hostIPAddress, 8888); 
     int requestCount = 0; 
     TcpClient clientSocket = default(TcpClient); 
     serverSocket.Start(); 
     Console.WriteLine(" >> Server Started"); 
     clientSocket = serverSocket.AcceptTcpClient(); 
     Console.WriteLine(" >> Accept connection from client"); 
     requestCount = 0; 

     while ((true)) 
     { 
      try 
      { 
       requestCount = requestCount + 1; 
       NetworkStream networkStream = clientSocket.GetStream(); 
       byte[] bytesFrom = new byte[10025]; 
       networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize); 
       string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom); 
       dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$")); 
       Console.WriteLine(" >> Data from client - " + dataFromClient); 
       string serverResponse = "Last Message from client" + dataFromClient; 
       Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse); 
       networkStream.Write(sendBytes, 0, sendBytes.Length); 
       networkStream.Flush(); 
       Console.WriteLine(" >> " + serverResponse); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.ToString()); 
      } 
     } 

     clientSocket.Close(); 
     serverSocket.Stop(); 
     Console.WriteLine(" >> exit"); 
     Console.ReadLine(); 

客戶端程序

try { 
     TcpClient tcpclnt = new TcpClient(); 
     Console.WriteLine("Connecting....."); 

     tcpclnt.Connect("192.168.128.1",8888); 
     // use the ipaddress as in the server program 

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

     String str=Console.ReadLine(); 
     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(); 
    } 

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

使用搜索。您必須監聽本機已分配給任何網卡的IP,除非您的調制解調器處於網橋模式,否則您有內部IP地址,最有可能是'192.168.x.x'。只要綁定到'0.0.0.0'。 – CodeCaster

+0

'TcpClient clientSocket = default(TcpClient);'是多餘的。當你實際分配變量'TcpClient clientSocket = serverSocket.AcceptTcpClient();' –

回答

0
IPAddress hostIPAddress = IPAddress.Parse("178.189.27.85"); 

是您的計算機實際上是公共互聯網上?我希望你的電腦有一個私人IP(例如192.168.x.y,172.16-32.x.y,10.x.y.z),除非它直接連接到你的互聯網連接。

假設你使用的是Windows,使用命令提示符並運行ipconfig,或訪問控制面板,查看您的網絡設置。在Windows 7上,例如它位於網絡和共享中心的「更改適配器設置」下;尋找活動適配器,右鍵單擊並選擇「狀態」。

找到本地IP地址後,請使用它。或者,您可以使用0.0.0.0作爲通配符地址 - 實際上是說「只要接受來自任何地方的連接,我都不在乎我的IP地址是什麼。」

+0

時,我剛剛聲明瞭TcpClient我使用了IP配置IP現在我在客戶端程序上得到這個異常「連接嘗試失敗,因爲連接方沒有在一段時間後正確響應,或建立連接失敗,因爲連接的主機未能響應「 –

+0

這是一個不同的問題。你有沒有改變你的代碼綁定到0.0.0.0?或者其他一些地址?在你的客戶端程序中(你沒有提供代碼)是在同一臺計算機上運行它嗎? –

+0

不,我不是在同一臺計算機上運行它。它是一臺不同的計算機....上面我提到了客戶端程序 –

相關問題