In this chapter we shall learn about:
1. Introduction
2. API’s for creating a UDP server:
3. API’s for creating a UDP client:
4. API prototypes
5. Example: UDPServer.c
6. Example: UDPClient.c
The above image shows the sequence of steps for a Server and client to create a UDP client server communication.
For creating a UDP server:
1. Create a socket using “socket()”
2. bind to an address using “bind()”
3. Wait for the data to be received using “recvfrom()”
4. Send the data to client using “sendto()”
5. Close the connection using “close()”
For creating a UDP client:
1. Create a socket using “socket()”
2. bind to an address using “bind()”
3. Send the data to server using “sendto()”
4. Receive the data from the server using “recvfrom()”
5. Close the connection using “close()”
Important header files to be included in UDP socket programming:
#include <sys/socket.h> It contains the data structures required for socket #include <netinet/in.h> It has the constants and structures required for Internet domain address. #include <sys/types.h> It has definitions of number of data types used for system calls.
Now let us understand the API’s involved in creating a server and client.
Important API used in UDP socket programming:
1. Create a socket using “socket()” system call
int sockID =socket(family, type, protocol);
2. Closing a socket using “close()” system call.
status = close(sockid);
3. Important Data Structures for specifying address:
struct sockaddr{ unsigned short sa_family; // address family AF_INET char sa_sata[14]; // Family specific address information }
struct in_addr{ unsigned long s_addr; // internet address 32 bits }
struct sockaddr_in{ unsigned short sin_family; // address family AF_INET unsigned short sin_port; // assign the port struct in_addr sin_addr; // assign the ip address char sin_ero[8]; }
4. Assign address to a socket using “bind()” api
int status = bind (sockid, &addrport, size)
5. Send the data using “sendto()”:
int count = sendto(sockid, msg, msgLen, flags, &serverAddr, addrlen);
6. Receive the data using “recvfrom()”:
int count = recvfrom(sockid, recvBuf, bufLen, flags, &clientAddr, addrlen);
Code for UDP server:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> int main( ) { int port = 1234; int sockfd; struct sockaddr_in si_me, si_other; char buffer[1024]; socklen_t addr_size; sockfd = socket(AF_INET, SOCK_DGRAM, 0); memset(&si_me, '\0', sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(port); si_me.sin_addr.s_addr = inet_addr("127.0.0.1"); bind(sockfd, (struct sockaddr*)&si_me, sizeof(si_me)); addr_size = sizeof(si_other); recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)& si_other, &addr_size); printf("[+]Data Received: %s", buffer); return 0; }
Code for UDP client:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> int main() { int port = 1234; int sockfd; struct sockaddr_in serverAddr; char buffer[1024]; socklen_t addr_size; sockfd = socket(PF_INET, SOCK_DGRAM, 0); memset(&serverAddr, '\0', sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(port); serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); strcpy(buffer, "Hello Server\n"); sendto(sockfd, buffer, 1024, 0, (struct sockaddr*)&serverAddr, sizeof(serverAddr)); printf("[+]Data Send: %s", buffer); return 0; }
Output:
[+]Data Received: Hello Server