I'm trying to send a UDP command to a device and receive a UDP response from that same device. The sending works fine. I can see the datagram depart (via WireShark). I can also see the datagram return from the device (again, via WireShark). The turnaround time between command departure and response reception is about 15 milliseconds.
Code
Byte[] button_click(Byte[] command)
{
// Device exists at a particular IP address and listens for UDP commands on a particular port
IPEndPoint SendingEndpoint = new IPEndPoint(DEVICE_IP, DEVICE_PORT);
// Device always sends from port 32795 to whatever port the command originated from on my machine
IPEndPoint ReceivingEndpoint = new IPEndPoint(DEVICE_IP, 32795);
// Sending client
sendingClient = new UdpClient();
sendingClient.Connect(SendingEndpoint);
// Receiving client
receivingClient = new UdpClient();
receivingClient.Client.ReceiveTimeout = RECEIVE_TIMEOUT; // timeout after 4 seconds
receivingClient.Connect(receivingEndpoint);
// Send command and wait for response
Byte[] response = null;
try
{
sendingClient.Connect(DEVICE_IP, DEVICE_PORT);
sendingClient.Send(command, command.Length);
response = receivingClient.Receive(ref receivingEndpoint);
}
catch (SocketException e)
{
// If we timeout, discard SocketException and return null response
}
return response;
}
Problem
I cannot capture the received datagram in my application. When I run the above code, I get the following exception:
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."
There are similar posts on StackOverflow, but none of them appear to address my situation. And I've verified that my packets are not being swept up in my firewall.
What am I doing wrong?