日期:2014-05-17 浏览次数:20908 次
int port = int.Parse(textBox2.Text); textBox1.AppendText("连接等待........."); UdpClient receivingUdpClient = new UdpClient(port);//创建UDPClient对象 IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, port);//创建IPEndPoint对象 while(true) { Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);//接收远程发送的UDP数据报,就是卡在这儿了 if (receiveBytes.Length > 0) { string returnData = Encoding.ASCII.GetString(receiveBytes);//将字节数组转换为字符串 textBox1.AppendText("This is the message you received " +//输出字符串信息 returnData.ToString()); textBox1.AppendText("This message was sent from " +//输出远程主机地址及发送数据报使用的端口 RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString()); DateTime currentTime = DateTime.Now; string strtime = currentTime.Hour.ToString() + ":" + currentTime.Minute.ToString() + ":" + currentTime.Second.ToString() + ":" + currentTime.Millisecond.ToString(); byte[] mybyte = Encoding.Default.GetBytes(strtime); receivingUdpClient.Send(mybyte, strtime.Length, RemoteIpEndPoint); } } }