You are on page 1of 1

The first few steps of the UDP client algorithm are much like the corresponding steps of the

TCP client algorithm. A UDP


client obtains the server address and protocol port number, and then allocates a socket for communication.

6.19 Connected And Unconnected UDP Sockets

Client applications can use UDP in one of two basic modes: connected and unconnected. In connected mode, the client
uses the connect call to specify a remote endpoint address (i.e., the server's IP address and protocol port number). Once it has
specified the remote endpoint, the client can send and receive messages much like a TCP client does. In unconnected mode, the
client does not connect the socket to a specific remote

endpoint. Instead, it specifies the remote destination each time it sends a message. The chief advantage of connected UDP
sockets lies in their convenience for conventional client software that interacts with only one server at a time: the application
only needs to specify the server once no matter how many datagrams it sends. The chief advantage of unconnected sockets lies in
their flexibility; the client can wait to decide which server to contact until it has a request to send. Furthermore, the client can
easily send each request to a different server.

UDP sockets can be connected, making it convenient to interact with a specific server, or they can be
unconnected, making it necessary for the application to specify the server's address each time it sends a
message.

6.20 Using Connect With UDP

Although a client can connect a socket of type SOCK_DGRAM, the connect call does not initiate any packet exchange, nor
does it test the validity of the remote endpoint address. Instead, it merely records the remote endpoint information in the socket
data structure for later use. Thus, when applied to SOCK_DGRAM sockets, connect only stores an address. Even if the
connect call succeeds, it does not mean that the remote endpoint address is valid or that the server is reachable.

6.21 Communicating With A Server Using UDP

After a UDP client calls connect, it can use write to send a message or read to receive a response. Unlike TCP, UDP
provides message transfer. Each time the client calls write, UDP sends a single message to the server. The message contains all
the data passed to write. Similarly, each call to read returns one complete message. Assuming the client has specified a
sufficiently large buffer, the read call returns all the data from the next message. Therefore, a UDP client does not need to make
repeated calls to read to obtain a single message.

57