The Linux Channel

TUTORIALS :: UDP sample socket code for Systems and Network software developers

Written by: Kiran Kankipati - Published: 02-Feb-2017


Language :: Portuguese

Here is my sample UDP socket code written in C. Unlike other versions you get commonly on-line this version is focused towards systems and network software developers and as a quick reference even for experienced software programmers. In this example/template, I am sending test raw binary data bytes from UDP sample client to registered UDP sample server. The code has documentation already in the form of comments (as you can see below). You can download my source below and you can use it directly in your projects or you can use the same for learning purposes to understand about user-space socket programming via IPv4 UDP transport layer protocol.

UDP is very unique, versatile and flexible unlike TCP, hence this can be used as internal device IPC mechanism instead using more traditional platforms such as message queues, pipes, etc. Unlike other IPC mechanisms, this provides a transparent fool-proof way to debug your software via Wireshark packet capture and allows you to fix the issues by going through the raw binary packet capture data.

UDP as a future high-speed network communication platform: UDP is also experimented to be used as a high-speed communication career protocol unlike TCP. TCP restricts advance Network software architects to use TCP provided congestion control, out-of-order packet sequence, features. But UDP provides a raw access to the network, so that advance users can architect these utilities in the user-space, application layer protocol(s). For example for the same obvious reasons Google invented QUIC protocol, which uses UDP as its career protocol in Google's Chrome browser instead TCP based HTTPS/SSL protocol. QUIC has its own user-space application layer reliability layer which ensures situations like packet loss, security, congestion control and so on. And with QUIC Google can also multiplex multiple remote connections/sessions into one communication channel there by improving network performance and user experience.

Here is my very detailed live demo video of the same, if you are interested kindly watch my video so that you can get the full bigger perspective. I have also discussed all the above points in-depth.


* Click the image above to watch this video on Youtube ↗

Here is my source-code of my test UDP socket client. Download: udp_socket_client.c

/* udp_socket_client.c
 * The Linux Channel
 * Author: Kiran Kankipati
 * Updated: 01-Feb-2017
 */

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define MAXBUF 1024

int main()
{ int sockfd, len, i;
  unsigned char buf[MAXBUF];
  struct sockaddr_in udp_server_addr;
  socklen_t addr_size;

  sockfd = socket(PF_INET, SOCK_DGRAM, 0); // create a UDP socket
  if(sockfd<=0) { printf("socket error !\n"); return 0; }

  /* configure settings to communicate with remote UDP server */
  udp_server_addr.sin_family = AF_INET;
  udp_server_addr.sin_port = htons(6800); // server port
  udp_server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // local-host
  memset(udp_server_addr.sin_zero, '\0', sizeof udp_server_addr.sin_zero);  
  addr_size = sizeof udp_server_addr;

  memset(buf, 0xab, MAXBUF); // set the entire buffer with 0xab (i.e 1010 1011 binary)

  sendto(sockfd, buf, MAXBUF, 0, (struct sockaddr *)&udp_server_addr, addr_size); //send the data to server
  
  len = recvfrom(sockfd, buf, MAXBUF, 0, NULL, NULL); // receive data from server
  
  printf("Received from server: %d bytes\n", len); for(i=0;i<len;i++) { printf("%02x ", buf[i]); } printf("\n"); //print received data dump

  close(sockfd); //close socket file-descriptor
  
  return 0;
}

Compile the udp_socket_client.c:

kiran@WD-1TB2:/code/thelinuxchannel/udp_socket$ gcc -o udp_socket_client udp_socket_client.c
kiran@WD-1TB2:/code/thelinuxchannel/udp_socket$


Here is my source-code of my test UDP socket server. Download: udp_socket_server.c

/* udp_socket_server.c
 * The Linux Channel
 * Author: Kiran Kankipati
 * Updated: 01-Feb-2017
 */

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define MAXBUF 1024

int main()
{ int sockfd, len, i;
  unsigned char buf[MAXBUF];
  struct sockaddr_in udp_server_addr;
  struct sockaddr_in udp_client_addr;
  struct sockaddr_storage server_storage;
  socklen_t addr_size, client_addr_size;

  sockfd = socket(PF_INET, SOCK_DGRAM, 0); // create a UDP socket
  if(sockfd<=0) { printf("socket error !\n"); return 0; }

  /* configure settings to register UDP server */
  udp_server_addr.sin_family = AF_INET;
  udp_server_addr.sin_port = htons(6800); // server port
  udp_server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // local-host
  memset(udp_server_addr.sin_zero, '\0', sizeof udp_server_addr.sin_zero);  
  bind(sockfd, (struct sockaddr *) &udp_server_addr, sizeof(udp_server_addr)); // bind the socket
  addr_size = sizeof server_storage;
  
  len = recvfrom(sockfd, buf, MAXBUF, 0, (struct sockaddr *)&server_storage, &addr_size); //receive data from client 
  
  printf("Received from server: %d bytes\n", len); for(i=0;i<len;i++) { printf("%02x ", buf[i]); } printf("\n"); //print received data dump
	
  memset(buf, 0xcd, MAXBUF); // set the entire buffer with 0xcd (i.e 1100 1101 binary)

  sendto(sockfd, buf, MAXBUF, 0, (struct sockaddr *)&server_storage, addr_size); //send the data to client

  return 0;
}

Compile the udp_socket_server.c:

kiran@WD-1TB2:/code/thelinuxchannel/udp_socket$ gcc -o udp_socket_server udp_socket_server.c
kiran@WD-1TB2:/code/thelinuxchannel/udp_socket$


Execute the binaries:

kiran@WD-1TB2:/code/thelinuxchannel/udp_socket$ ls
udp_socket_client  udp_socket_client.c  udp_socket_server  udp_socket_server.c
kiran@WD-1TB2:/code/thelinuxchannel/udp_socket$

First execute the udp_socket_server in one terminal so that it registers and listens on the port 6800 as shown below:
execute udp server in the terminal

Now on a different terminal check the status of the UDP server registration via netstat -l command:
with netstat check the status of udp server registration

Before executing the UDP client, open Wireshark and capture the local loopback port lo as shown below:
before executing udp client open wireshark and capture local loopback port

Execute the UDP socket client on a different terminal as shown below. You can see as soon as it gets executed, it sends the test data to the UDP socket server and receives the test data from the server.
execute udp socket client in a different terminal

Here is the screenshot of the UDP socket server execution terminal. UDP socket server receives the test data from client prints the same on the screen (as shown below) and quickly responds to the client by sending its own test data dump.
received data in udp server from udp client

In the Wireshark packet capture you can see these packets, analyze and confirm the same. Below is the Wireshark packet capture of UDP packet from client to server:
wireshark capture of udp packet client to server

And here is the Wireshark packet capture of UDP packet from server to client:
wireshark capture of udp packet server to client

Linux Courses Offered: If you are interested to join classes on Networking, Linux, Systems software, etc with live demo training sessions. You can learn via distance learning platform. I teach my students in old fashioned way the "Guru-Shishya" model. And I believe this is how the education should be provided with no strings attached and boundaries. For more details visit COURSES.


Featured Video:


* Click the image above to watch this video on Youtube ↗


Suggested Topics:


Network Software Development:
 UDP sample socket code for Systems and Network software developers ↗
02-Feb-2017


 iPerf tool - for network channel analysis, bandwidth tests and system & network benchmarking ↗
04-Jul-2018


 Tail Drop - by Network Schedulers to Drop Packets in Network Appliances ↗
27-Jun-2018


 Bufferbloat in a Networking Device or an Appliance ↗
27-Jun-2018


 C code to check valid IP Address (IPv4) - Live Demo and Example ↗
18-Jun-2018



Research and Projects:
 Data Profiling and Visual Analysis tool for Deep Learning and AI - VLOG - The Linux Channel ↗
21-Jun-2018


 CYRIL INGÉNIERIE - CoreFreq Linux CPU monitoring software ↗
19-Jun-2018


 Difference between Intel Xeon Server vs Desktop Core i7 vs Intel Mobile Atom vs ARM Processors ↗
19-Jun-2018


 Research Socket overhead in Linux vs Message Queues and benchmarking ↗
21-Jun-2017


 Tour of my new Home Networking Lab within VirtualBox VM ↗
16-May-2017



Linux (user-space), Systems Architecture:
 Linux user-space Pthread Synchronization via Mutex - Live Demo and Example ↗
03-Jun-2018


 Coding a simple look-up-table in C - without Linked lists and a binary search ↗
17-Jun-2018


 Synchronization in Linux user-space - Architecting multi-process and multi-threads ↗
18-Sep-2018


 A Linux system call in C without a standard library ↗
10-Mar-2017


 C code to read a /proc file in Linux user-space - Live Demo and Example ↗
11-Jun-2018


 Linux user-space - Shared Memory IPC - Live Demo and Example ↗
18-Jul-2018


 Dynamic linking without STDLIB in Linux user-space C programming ↗
05-May-2017


 Linux user-space Atomic Operations via GCC Atomic builtins ↗
22-Mar-2017



Linux Kernel Software Development:
 Kernel customization via make menuconfig - Linux Kernel Compilation (or a Kernel Build) ↗
11-Jul-2018


 Linux Networking Stack :: RFS: Receive Flow Steering ↗
09-Dec-2017


 How to submit your Linux Kernel Patch ↗
07-Feb-2017


 How to upgrade or install a latest current release Linux Kernel version manually in Ubuntu? ↗
05-Jan-2018


 Tracking Linux Kernel bugs in Kernel.org Bugzilla (or Bugzee as we call it in the Industry) ↗
24-Sep-2018



Software Development (Programming) Tools:
 Kompare GUI patch and diff tool in Linux ↗
19-Jun-2018


 Bluefish IDE - a simple versatile light-weight best editor to edit kernel source files, html files, c source code, text files, SQL schema and so on. ↗
19-Jun-2018



Embedded Projects:
 Using a GPS Sensor ↗
28-Jul-2017


 GPS Geo-tracking system using LinkIt ONE ↗
11-Jan-2017


 

Join The Linux Channel :: Facebook Group


Join a course:

💎 Linux Kernel and Systems Software: PDF Brochure

💎 Linux and Network Software Developer: PDF Brochure


💗 Help shape the future: Sponsor/Donate


Recommended Topics:
Featured Video:

* Click the image above to watch this video on Youtube ↗


Trending Video:

* Click the image above to watch this video on Youtube ↗

TUTORIALS :: UDP sample socket code for Systems and Network software developers:


Linux Kernel Programming - Device Drivers:


Research :: Content Delivery Networks (CDN):
CDN Networks


Recommended Video:

* Click the image above to watch this video on Youtube ↗

The TOFFEE Project - v10.7 :: Updated: 10-Nov-2018 :: © 2018 :: Author: Kiran Kankipati
Your IP: 54.163.20.57 :: Browser: CCBot/2.0 (https://commoncrawl.org/faq/)