Important: |
---|
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
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.
-
Open a datagram socket with socket.
Use AF_INET for the address formatparameter and SOCK_DGRAMfor the typeparameter.
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.
-
Name the socket with the bindfunction, using a SOCKADDR_IN structure for the addressparameter.
-
Exchange data with a client using the sendtoand recvfromfunctions.
The UDP datagram socket server application calls recvfromto prepare to receive data from a client The recvfromfunction 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 sendtofunction is used on a connectionless socket to send a datagram to a specific peer socket identified by the toparameter. Successfully completing a sendtofunction call does not confirm data was successfully delivered.
-
Close the connection with the closesocketfunction.
Calling the shutdownfunction is unnecessary for UDP sockets.
-
Open a socket with the socketfunction.
-
Exchange data with server using sendtoand recvfrom.
A UDP datagram client socket is named when the client calls sendto.
-
Close the connection with the closesocketfunctions.
Calling shutdownis unnecessary for UDP sockets.