a_confused_student
a_confused_student

Reputation: 405

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

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 :)

Upvotes: 3

Views: 8546

Answers (1)

dbush
dbush

Reputation: 223897

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.

Upvotes: 7

Related Questions