Analyzing the connected UDP Socket code example

The connected UDP socket code example can be found here. An interesting things about this socket connection is that there is no accept/listen API on the server side as is present in a TCP stream socket. In an UDP socket, normally the client does not use connect API (though it can) but in this socket connection, connect API is used at both endpoints.

Both endpoints connect to one another and are aware of the address to connect to.

Endpoint 1 – which is basically the server side code in our example code opens a socket and binds to a port and IP address. The way the IP address interfaces were created can be seen here. Endpoint 1 code for socket creation, bind and connect is shown below

         sock_fd = socket(AF_INET, SOCK_DGRAM, 0);

        /* fill endpoint 1 port and IP address and bind to endpoint 1*/

        servaddr->sin_family = AF_INET;
        servaddr->sin_port = PORT;
        servaddr->sin_addr.s_addr = inet_addr(“192.168.0.11”);

        bind(sock_fd, (struct sockaddr *)servaddr, sizeof(struct sockaddr_in));

        /*connect to peer IP address and port */

        client_addr->sin_family = AF_INET;
        client_addr->sin_port = PORT_CLIENT;
        client_addr->sin_addr.s_addr = inet_addr(“192.168.0.14”);

        connect(sock_fd, (struct sockaddr *)client_addr, sizeof(struct sockaddr_in));

Similarly, the endpoint 2 – opens a socket, binds to an IPaddress/port and connects to the peer IP address port

         sockfd = socket(AF_INET, SOCK_DGRAM, 0);

        /*fill endpoint 2 – IP address and port to bind to */

        clientaddr->sin_family = AF_INET;
        clientaddr->sin_port = PORT;
        clientaddr->sin_addr.s_addr = inet_addr(“192.168.0.14”);

        bind(sockfd, (struct sockaddr *)clientaddr, sizeof(struct sockaddr_in));

        /* connect to peer IP address and port */

        serveraddr->sin_family = AF_INET;
        serveraddr->sin_port = SERVER_PORT;
        serveraddr->sin_addr.s_addr = inet_addr(“192.168.0.11”);

        connect(sockfd, (struct sockaddr *)serveraddr, sizeof(struct sockaddr_in));

Now, both endpoints are connected to one another and the sample output of the “ss shell command” provides the below output.

Netid  State     Recv-Q  Send-Q Local Address:Port   Peer Address:Port

udp     ESTAB  45312     0           192.168.0.11:20675    192.168.0.14:20931
udp     ESTAB  44544     0           192.168.0.14:20931     192.168.0.11:20675

The state of the socket connection is shown as established as in contrast to the earlier UDP examples, wherein they were in unconnected state.

Oddly, the port that is provided is not the one provided in the program but a port assigned by the operating system (something for me to investigate further 🙂 )

NOTE: The sample code needs to be modified to a while (1) transmit/recv block to find the output of the “ss” command, otherwise, the program will exit before the “ss” shell command can show the output. I leave the task of changing the sample code to a while (1) tx/rx as an exercise to the reader.

Since both endpoints are connected to one another, APIs like send/recv can be used instead of sendto/recvfrom APIs as is normally used for an UDP socket.

  Abstract Namespace Socket connections

Comments

  1. Pingback: The Connected UDP Socket Code Example | Hitch Hiker's Guide to Learning

Leave a Reply

Your email address will not be published. Required fields are marked *