我要完成一个接收远程计算机的发往本机60000端口的UDP数据并用本机的60000端口向远程计算机的60000端口发回一个UDP数据的功能,代码大致如下:remoteip= IPAddress.Parse("192.168.1.1"); //set ipaddr
port = 60000;
ipe = new IPEndPoint(remoteip, port); //IPEndPoint
host = new UdpClient(port);
while (true)
{
////Receive udp data from host
Byte[] byteReceived = host.Receive(ref ipe);
////Send and udp to host
string request = "Keepalive packet";
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
host.Send(bytesSent, bytesSent.Length, ipe);
}
现在的问题是如果单独使用收或发,则程序运行正常,但是在收数据后发数据,在第二次接收数据时则会产生异常,说是远程计算机强行断开现有连接,请问这是怎么回事?
port = 60000;
ipe = new IPEndPoint(remoteip, port); //IPEndPoint
host = new UdpClient(port);
while (true)
{
////Receive udp data from host
Byte[] byteReceived = host.Receive(ref ipe);
////Send and udp to host
string request = "Keepalive packet";
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
host.Send(bytesSent, bytesSent.Length, ipe);
}
现在的问题是如果单独使用收或发,则程序运行正常,但是在收数据后发数据,在第二次接收数据时则会产生异常,说是远程计算机强行断开现有连接,请问这是怎么回事?
host.Send(bytesSent, bytesSent.Length)试试,要是不行,就先用Connect建立一个连接吧
还是不行,在Send用Connect产生同样的异常