difference between send() and sendTo() in C for a UDP network implementation

Viewed 1307

I'm trying to implement a UDP network between a client and a server but in many implementations, they use either send() or sendTo() I tried looking in the man pages but I didn't really understand the difference other than that the sendTo() takes in more arguments which makes it look rather useless compared to send(). If you could bring any clarity on this matter I would be happy to hear :)

1 Answers

The sendto function is the one that's generally used for UDP sockets. As UDP is connectionless, this function allows you to specify the IP and port that each outgoing packet is sent to.

You can however also use send if you first use connect. The connect function can be used to specify the destination IP and port for all packets sent using send. It also restricts the packets you receive to just those from that IP/port. The connect function may be called multiple times to change the associated remote IP/port, or to remove the association.

In general, I would recommend sticking with sendto as it gives you more flexibility over who you're sending to.

Related