Creating a connectionless socket
Connectionless sockets do not establish a connection over which
data is transferred. Instead, the server application specifies its name where
a client can send requests.
Connectionless sockets use User Datagram Protocol (UDP) instead
of TCP/IP.
The following figure illustrates the client/server relationship of the socket APIs used in the examples for a connectionless socket design.
The following figure illustrates the client/server relationship of the socket APIs used in the examples for a connectionless socket design.
Socket flow of events: Connectionless server
The following sequence of the socket calls provides a description of the figure and the following example programs. It also describes the relationship between the server and client application in a connectionless design. Each set of flows contains links to usage notes on specific APIs. If you need more details on the use of a particular API, you can use these links. The first example of a connectionless server uses the following sequence of API calls:- The socket() API returns a socket descriptor, which represents an endpoint. The statement also identifies that the Internet Protocol address family (AF_INET) with the UDP transport (SOCK_DGRAM) is used for this socket.
- After the socket descriptor is created, a bind() API gets a unique name for the socket. In this example, the user sets the s_addr to zero, which means that the UDP port of 3555 is bound to all IPv4 addresses on the system.
- The server uses the recvfrom() API to receive that data. The recvfrom() API waits indefinitely for data to arrive.
- The sendto() API echoes the data back to the client.
- The close() API ends any open socket descriptors.
Socket flow of events: Connectionless client
The second example of a connectionless client uses the following sequence of API calls.- The socket() API returns a socket descriptor, which represents an endpoint. The statement also identifies that the Internet Protocol address family (AF_INET) with the UDP transport (SOCK_DGRAM) is used for this socket.
- In the client example program, if the server string that was passed into the inet_addr() API was not a dotted decimal IP address, then it is assumed to be the host name of the server. In that case, use the gethostbyname() API to retrieve the IP address of the server.
- Use the sendto() API to send the data to the server.
- Use the recvfrom() API to receive the data from the server.
- The close() API ends any open socket descriptors.
- Example: A connectionless server
This example illustrates how to create a connectionless socket server program by using User Datagram Protocol (UDP). - Example: A connectionless client
This example shows how to use User Datagram Protocol (UDP) to connect a connectionless socket client program to a server.
Parent topic: Basic socket design
No comments:
Post a Comment
Thank you