1
votes

I am creating a UDP socket client in C (unicast) and is wondering why recvfrom() has a struct sockaddr * argument in which in the man page says, A null pointer, or points to a sockaddr structure in which the sending address is to be stored.

Is it possible that I could receive a message from a different server other than the one I sendto? If yes, how to create this scenario?

If no, is it correct to say that this argument is only useful when broadcast mode is used?

3

3 Answers

2
votes

Yes, this is perfectly possible. The reason for this is that UDP is not stream-based, but packet-based. Every packet is treated without any history (other packets sent or received).

For this reason you may also open a UDP port and then send packets to different hosts from it. However, I do not remember how well this is supported by the API.

0
votes

The UDP socket will recvfrom() any host sending to this one with correct port unless you explicitly connect(), in which case you can just write() and read(), and get errors upon received ICMP messages.

-1
votes

Considering you always have two parties in UDP, it seems rather obvious that someone has to recvfrom() first.