Creating a UDP Datagram Socket Application

A datagram socket uses UDP, an unreliable connectionless protocol. A UDP server does not have to listen for and accept client connections, and a UDP client does not have to connect to a server. The following illustration shows the interaction between the UDP server and UDP client.

    To create a UDP datagram socket server application

  1. Open a datagram socket with socket.

    Use AF_INET for the address format parameter and SOCK_DGRAM for the type parameter.

    To prepare for association with a client, a UDP datagram server need only create a socket and bind it to a name when preparing for association with a client.

  2. Name the socket with the bind function, using a SOCKADDR_IN structure for the address parameter.
  3. Exchange data with a client using the sendto and recvfrom functions.

    The UDP datagram socket server application calls recvfrom to prepare to receive data from a client The recvfrom function reads incoming data on unconnected sockets and captures the address from which the data was sent. To do this, the local address of the socket must be known.

    The sendto function is used on a connectionless socket to send a datagram to a specific peer socket identified by the to parameter. Successfully completing a sendto function call does not confirm data was successfully delivered.

  4. Close the connection with the closesocket function.

    Calling the shutdown function is unnecessary for UDP sockets.

    To create a UDP datagram socket client application

  1. Open a socket with the socket function.
  2. Exchange data with server using sendto and recvfrom.

    A UDP datagram client socket is named when the client calls sendto.

  3. Close the connection with the closesocket functions.

    Calling shutdown is unnecessary for UDP sockets.