Chapter 4: Establishing UDP Connections
How to send and receive User Datagram Protocol packets.
How UDP sockets differ #
- It uses the
SOCK_DGRAMsocket type. - Using
connect()can be optional with the use ofsendto()andrecvfrom(). - Sockets in TCP requires managing a socket for each peer connection while UDP needs only socket to communicate with any number of peers.
listen()andaccept()are not required in UDP since it only needs to bind to a local address which can then immediately send and receive data.
UDP Client Flow #
With connect() #
getaddrinfo()socket()connect()send()recv()close()
Without connect() #
getaddrinfo()socket()sendto()recvfrom()close()
UDP Server Flow #
getaddrinfo()socket()bind()sendto()recvfrom()close()