I know that recvfrom
can return the source address via a pointer argument so that I can check whether the received packet is from an address I'm interested in. Is there any way other than checking the source address only after a packet has already been received?
Asked
Active
Viewed 700 times
1

xiaokaoy
- 1,608
- 3
- 15
- 27
2 Answers
1
Yes. Just connect your socket to the desired source address. Datagrams from any other address will be thrown away.

user207421
- 305,947
- 44
- 307
- 483
-
Thanks. Can I 'connect' a peer with a specified IP address and ANY port number. I mean, I don't want to specify a port number, as I don't know the port number the peer might use as the source port number while sending packets to me. – xiaokaoy Apr 28 '17 at 03:24
-
You will know as soon as you receive a datagram from that source. I don't believe UDP connect supports an ANY port option. – user207421 Apr 28 '17 at 04:07
-1
It depends on the language. For example, Java's DatagramSocket has a connect method which drops all packets that don't belong to the specified address. All it really does it check if the source address is from the one specified like you say. It's simply how the UDP stack works.

jython234
- 315
- 2
- 9
-
The BSD Sockets API has the `connect()` method, and it is exposed in all languages I am aware of. – user207421 Apr 28 '17 at 02:46