简体   繁体   English

UdpClient接收未接收到数据报

[英]UdpClient Receive not receiving datagram

I trying to write a TFTP client for a class project. 我试图为一个类项目编写一个TFTP客户端。 Using the UdpClient class I can successfully request data from the server but the returned packet never reaches my code. 使用UdpClient类,我可以成功地从服务器请求数据,但是返回的数据包永远不会到达我的代码。

My firewall is turned off. 我的防火墙已关闭。 I can observe the returned packet in wireshark but UdpClient.Receive blocks indefinitely. 我可以在wirehark中观察返回的数据包,但是UdpClient.Receive无限期阻塞。

mUdpClient is initialized like this in the constructor: mUdpClient = new UdpClient(); mUdpClient在构造函数中像这样初始化: mUdpClient = new UdpClient();

mUdpClient is connected like this mUdpClient这样连接

public void connect(String host, int port) {
    mServerAddress = System.Net.Dns.GetHostAddresses(host)[0];
    var endPoint = new IPEndPoint(mServerAddress, port);
    mUdpClient.Connect(endPoint);
}

After the connect I send my request which is successful (as observed in wireshark) 连接后,我发送成功的请求(如wireshark所观察)

This is what my receive code looks like 这就是我的接收代码的样子

private void receiveResponse() {
    var newEndpoint = new IPEndPoint(IPAddress.Any, 0);
    byte[] response = mUdpClient.Receive(ref newEndpoint);
    Console.Out.WriteLine(response);
}

This has been tested on my Surface Pro and a Windows 8.1 VirtualBox VM running under Debian. 这已在我的Surface Pro和在Debian下运行的Windows 8.1 VirtualBox VM上进行了测试。

Note that since you are using the Connect() method on your UDP socket, you will only see datagrams actually sent from that IPEndPoint. 请注意,由于您正在UDP套接字上使用Connect()方法,因此您只会看到实际上是从该IPEndPoint发送的数据报。 If your remote host for some reason uses a different IPEndPoint to send data back to you, you won't see it. 如果您的远程主机出于某种原因使用其他IPEndPoint将数据发送回给您,您将看不到它。 So maybe try not using the default host feature (ie don't call Connect...just provide the remote IPEndPoint on each Send() call). 因此,也许尝试不使用默认的主机功能(即,不要调用Connect ...只需在每个Send()调用中提供远程IPEndPoint)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM