user1205577
user1205577

Reputation: 2460

C++ receive UDP packet on same port sent from

I have 2 UDP sockets (SOCKET), one for sending and one for receiving on a Windows machine. They both work well, but the trouble is that a program that is receiving messages from my send socket replies to the same port which sent the message.

I know that if I don't bind the send socket, using sendto will pick an ephemeral port to send on.

I'd like to know if it is possible to any of the following, and if so, what is the recommended way to do it:

So far I have tried:

Packets are being received from more than one outside entity, and not guaranteed to be in any particular order.

Thank you in advance!

Upvotes: 2

Views: 3356

Answers (1)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

Looks like you are trying to use threads to separate sending and receiving data. I would question this approach, since UDP is so easy to handle in one thread. Nevertheless, you can just use the same socket from both threads if you want (see related question: Are parallel calls to send/recv on the same socket valid?). Just bind(2) it and, optionally, connect(2) it.

Upvotes: 1

Related Questions