gh123man
gh123man

Reputation: 1414

UdpClient Receive not receiving datagram

I trying to write a TFTP client for a class project. Using the UdpClient class I can successfully request data from the server but the returned packet never reaches my code.

My firewall is turned off. I can observe the returned packet in wireshark but UdpClient.Receive blocks indefinitely.

mUdpClient is initialized like this in the constructor: mUdpClient = new UdpClient();

mUdpClient is connected like this

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)

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.

Upvotes: 0

Views: 1699

Answers (1)

Peter Duniho
Peter Duniho

Reputation: 70701

Note that since you are using the Connect() method on your UDP socket, you will only see datagrams actually sent from that IPEndPoint. If your remote host for some reason uses a different IPEndPoint to send data back to you, you won't see it. So maybe try not using the default host feature (i.e. don't call Connect...just provide the remote IPEndPoint on each Send() call).

Upvotes: 1

Related Questions