UdpClient, Receive() right after Send() does not work?
Asked Answered
B

2

6

Consider the following code:

client.Send(data, data.Length, endpoint);
byte[] response = client.Receive(ref endpoint);

While, according to WireShark (network sniffer), the remote host does reply with data, the application here just waits for data forever... it does not receive the answer from the remote host for some reason.

Any ideas?

Balladmonger answered 21/10, 2008 at 13:2 Comment(3)
Are you calling client.Connect()? If so, it will only accept data from the enpoint supplied in the Connect method.Aubade
Did you ever solve this?Tailgate
That was 10 years ago, as far as I can remember I went with @ageektrapped's solution and used to clients. These days I try to avoid using udp/tcp directly and prefer solutions like ZeroMQ/NetMQ combined with Protocol BuffersBalladmonger
W
6

You probably want to setup two UdpClients: one for listening, one for sending.

For the receiving UdpClient, use the constructor that takes a port.

Waltner answered 21/10, 2008 at 16:20 Comment(0)
T
1

probably the remote host has firewall then couldn't response to request, before send request set the

client.Client.ReceiveTimeout = 5000; 

so when the response couldn't get the request you have a exception

Tuckie answered 31/1, 2011 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.