HOMECOURSESSTUDENTSDONATIONSVIDEOSEVENTSTUTORIALSLINKSNEWSCONTACT


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

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.

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:

[email protected]:/code/thelinuxchannel/udp_socket$ gcc -o udp_socket_client udp_socket_client.c
[email protected]:/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:

[email protected]:/code/thelinuxchannel/udp_socket$ gcc -o udp_socket_server udp_socket_server.c
[email protected]:/code/thelinuxchannel/udp_socket$


Execute the binaries:

[email protected]:/code/thelinuxchannel/udp_socket$ ls
udp_socket_client  udp_socket_client.c  udp_socket_server  udp_socket_server.c
[email protected]:/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:



Suggested Topics:


☆ Tutorials :: Arduino UNO Projects ↗


☆ Tutorials :: Network Software Development ↗


☆ Tutorials :: Research and Projects ↗


☆ Tutorials :: Linux (user-space), Systems Architecture ↗


☆ Tutorials :: Linux Kernel Software Development ↗


☆ Tutorials :: Linux Kernel Internals (PDFs) - by Ramin Farajpour ↗


☆ Tutorials :: Software Development (Programming) Tools ↗


☆ Tutorials :: Embedded Projects ↗

Join The Linux Channel :: Facebook Group ↗

Visit The Linux Channel :: on Youtube ↗


💗 Help shape the future: Sponsor/Donate


Recommended Topics:
Featured Video:
Watch on Youtube - x20f Code with Kiran - Live Coding | Linux Kernel Programming | Kernel Libraries | Part 1 ↗

Multi-Client TCP Server with pthreads Mutex Synchronized - Code-walk of viewer code - Sekhar Pariga ↗
Monday' 28-Sep-2020
Here is a code-walk of a git-hub source submitted by a viewer Sekhar Pariga, which is a Multi-client TCP Server with pthreads Mutex synchronized. Basically it is a Directory Listing Server, that serves ls, cd, pwd of directory functions to connecting TCP clients which are remotely (locally) executed by the server. Server is implemented using multi-thread pthread library, to serve multiple clients simultaneously. Server keeps the each accepted client session details in the queue data structure. Client session queue contains each clients socket-fd and present working directory as a queue node.

Adding your own Kernel Modules into Linux Kernel Source | Linux Kernel Programming ↗
Monday' 28-Sep-2020
Whenever you do custom kernel modules, you can optionally make it a part of existing Linux Kernel source. This does not mean you are submitting your kernel module to the mainline kernel source (i.e kernel.org Linux Kernel Foundation). What I meant is, you can make your kernel module(s) part of Linux Kernel source so that when you compile your kernel you can automatically compile your kernel module(s) too. As well when you create/modify kernel .config configuration file (such as via make menuconfig, etc), you can enable or disable your kernel module(s) too.
To do the same you have to register (and include) your custom Kernel Module's Kconfig and Makefile to the existing Kconfig and Makefile of the Linux Kernel source Here is a detailed multi-episode video of mine which gives the overall idea and the big-picture.

Roadmap - How to become Linux Kernel Developer - Device Drivers Programmer and a Systems Software Expert ↗
Monday' 28-Sep-2020
Many viewers and even sometimes my students ask me how I can become a kernel programmer or just device driver developer and so on. So I shot this video (and an add-on video) where I summarized steps and a roadmap to become a full-fledged Linux Kernel Developer.

Weekly News Digest - Week 03 - July 2020 ↗
Monday' 28-Sep-2020
The Linux Channel :: Weekly News Digest - Week 03 - July 2020
> Linux founder tells Intel to stop inventing 'magic instructions' and 'start fixing real problems'
> QNAP launches its first 2.5GbE network switch - QSW-1105-5T
> Japan's ARM-based Fugaku is the world's fastest supercomputer
> FreeBSD Back To Seeing Progress On 802.11ac WiFi Support, Ath10k Driver
> Sparkfun Launches the ZED-F9R GPS Dead Reckoning Raspberry Pi pHAT for Mobile Robots
> ODROID-N2 Plus SBC Gets Amlogic S922X Rev. C Processor Clocked at up to 2.4 GHz
> Rock Pi E SBC Comes with WiFi, Bluetooth, Two Ethernet Ports, and Optional PoE

Nmap Network Scanning ↗
Monday' 28-Sep-2020

Slow RGB Fade via Arduino UNO ↗
Monday' 28-Sep-2020

PHP installation ↗
Monday' 28-Sep-2020

Layer-2 (L2) Multicast Frame Deep Analysis ↗
Monday' 28-Sep-2020

Management ↗
Monday' 28-Sep-2020

IP-in-IP Tunneling Demystified - VPN Tunnels ↗
Monday' 28-Sep-2020
There can be many ways one can architect VPN Networks. The end objective is to tunnel private IP (typically LAN subnets) within public IP Networks (such as Internet). You can establish such a tunnel in various creative ways, such as Transport Mode, Tunnel Mode, IP-in-IP tunneling, etc. Also you can choose any transport layer protocol of your choice such as send VPN traffic via TCP, or via UDP and so on.


Trending Video:
Watch on Youtube - x20f Code with Kiran - Live Coding | Linux Kernel Programming | Kernel Libraries | Part 1 ↗

UDP sample socket code for Systems and Network software developers ↗
Monday' 28-Sep-2020
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.



Recommended Video:
Watch on Youtube - x20f Code with Kiran - Live Coding | Linux Kernel Programming | Kernel Libraries | Part 1 ↗