2015-06-13 64 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

不,我不是在同一台计算机上运行它。它是一台不同的计算机....上面我提到了客户端程序 –

相关问题