Linux – Using write()/read() on UDP socket

clinuxsocketsudp

According to the man pages:

The only difference between send() and write(2) is the presence of flags. With a zero flags argument, send() is equivalent to write(2). Also, the following call send(sockfd, buf, len, flags); is equivalent to sendto(sockfd, buf, len, flags, NULL, 0);

and

The recv() call is normally used only on a connected socket (see connect(2)) and is identical to recvfrom() with a NULL src_addr argument.

Also, if I'm not wrong (couldn't find it in the man pages), recv with flags == 0 is equivalent to read (analogue to write and send).


So:

  • does this mean, that using readon a UDP socket is perfectly fine (if I don't need the src_addr)?
  • is there a way to use write on UDP socket (as now I set the destination address in sendto's dest_addr parameter)?

Best Answer

  • Using read() on a UDP socket is perfectly fine if you don't need the source address.
  • You can use write() if you connect() the UDP socket to the destination.
Related Topic