日期:2014-05-17  浏览次数:20550 次

tcp/ip通信时报错,请大家给看看
服务端
  socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
   
  IPAddress ipaddress = IPAddress.Parse(txtIP.Text.Trim()); //获取文本框输入的IP地址
   
  IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(txtPORT.Text.Trim())); /输
  //监听绑定的网络节点
  socketWatch.Bind(endpoint);  
  socketWatch.Listen(20);  
  threadWatch = new Thread(WatchConnecting);  
  threadWatch.IsBackground = true;
  //启动线程
  threadWatch.Start();
  //启动线程后 txtMsg文本框显示相应提示
  txtMsg.AppendText("开始监听客户端传来的信息!" + "\r\n");

客户端 [color=#FF0000][/color]socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  //需要获取文本框中的IP地址
  IPAddress ipaddress = IPAddress.Parse(txtIP.Text.Trim());
  //将获取的ip地址和端口号绑定到网络节点endpoint上
  IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(txtPort.Text.Trim()));
  //这里客户端套接字连接到网络节点(服务端)用的方法是Connect 而不是Bind
  socketClient.Connect(endpoint);
  //创建一个线程 用于监听服务端发来的消息
  threadClient = new Thread(RecMsg);
  //将窗体线程设置为与后台同步
  threadClient.IsBackground = true;
  //启动线程
  threadClient.Start();
我在服务端连接ip是192.168.0.3,触发button时,就报错socketWatch.Bind(endpoint),(在其上下文中,该请求的地址无效)
高手指教
 

------解决方案--------------------
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, int.Parse(txtPort.Text.Trim()))