[Sharing] Socket Programming 101 - UDP

In this article, we will have a basic understanding of Socket Programming first, then get into the detailed code of socket programming to further understand the difference between TCP and UDP transport protocol. Socket Programming is widely used in lower-level embedded systems such as Ameba (Amb21, 22, and 23) or BW16.

:mega: I have uploaded the code for testing on GitHub, feel free to clone the repository to your local environment and perform the test, so that you can have a better understanding of the code.

:link: GitHub - S10143806H/socket-programming-exercises: Develop a socket program in UNIX/Linux that uses TCP and UDP as the transport protocol for transferring message between a client and server.


What is Socket?

  • Used to do communication between 2 processes running on two different machines

  • An object (special file) acts as an endpoint between two machines

    image

UDP (Connectionless)

image

  • Here is the flow of socket programming flow used in UDP. UDP is connectionless because there is not necessary to wait for ACK from the receiver end once the sender sent any data packet.

  • Basically, the client-side will need to create a socket and using the SendTo() function transfer data to the server-side, then the socket will be closed after data is transmitted successfully.

  • While on the UDP server-side, similarly, a socket needed to be created first, then waiting for the binding procedure from the UDP client side. Upon the connection is established, the server is ready for data received using the ReceiveFrom() function. Upon the data has been correctly received, the socket on the server side will be closed as well.

  • Detailed illustration is showing in the graph below:
    image

Code Explanation

I have attached the socket programming code of the UDP client here, you can find code for UDP server, TCP client & server as well.

  1. On the UDP client side, the code can be categorized into 2 sections:

    int main(int argc, char *argv[])
    

    and user-defined function:

    void str_cli1(FILE *fp, int sockfd, struct sockaddr *addr, int addrlen, int *len)
    
    • In the main function, the socket is created using

      sockfd = socket(AF_INET, SOCK_DGRAM, 0);
      

      where 3 input parameters were taken, represented the Socket Family, Socket Type, and Protocol as illustrated in the image below:

      Some keywords used by the above 3 parameters are:
      image

    • In the str_cli1 takes File, socket, address structure, address length and data length as input
      parameters to execute the:

      sendto(sockfd, &sends, strlen(sends), 0, addr, addrlen);  // socket, data, data length, flag, address, 
      address length  
      

      function to send the packet to the server.

  2. For UDP Server, there are also two main functions:

    int main(int argc, char *argv[])
    

    and user-defined

    void str_ser1(int sockfd)
    
    • Inside the main() function, the socket will be created and waiting for the binding process from the client-side using an if loop:
      if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1) { 
        printf("error in binding");
        exit(1);
      }
      
    • the str_ser1() function contains the recvfrom() function to receive the packet sending from the UDP client. The input parameters taken by recvfrom() function are exactly the same as sendto().
       if ((n = recvfrom(sockfd, &recvs, MAXSIZE, 0, (struct sockaddr *)&addr, &len)) == -1) 
      

In the next article, we will compare the socket programming process in TCP and compare it with UDP processes.


Running Example Code

Step 1

All the example code has been uploaded to GitHub, feel free to download and read the code. If you have a Linux OS or you have installed Cygwin on Windows OS, you can modify the .c code and use the command below:

gcc udp_server.c -o upd_server

to generate a udp_server.exe executable file. Similarly, a udp_client.exe can also be generated.

Step 2

Upon the two executable files are generated successfully, you can use 2 terminals to execute server-side and client-side executable on two terminals using the command below:

.\udp_server
3 Likes