I am working in C, using the POSIX socket API.
I am not sure when it is appropriate to connect a datagram socket. As I understand it, UDP is connectionless, and SOCK_DGRAM sockets use UDP. So what happens when connect() and accept() are used on datagram sockets?
It seems to me that connecting them and using send()/recv() is easier than not connecting them and using sendto()/recvfrom().
Is there a difference in the functionality of the sockets when connected, or is this just an abstraction?
connect()
on a datagram socket is appropriate when you want the convenience of a default destination peer for use by send()
, rather than explicitly specifying a destination with sendto()
. There is no functional difference.
connect()
ing to an AF_UNSPEC address will clear the datagram socket's default peer. (This last behavior is widely supported, I think, but only recently formalized.)
connect()
on a datagram socket will limits recv()
s to data originating from the peer.
accept()
on a datagram socket is an error (EOPNOTSUPP), regardless of "connectedness".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With