UDP Echo Server-Client Program

In this section, we show a simple echo server/client program using UDP. For ease of reference, the program is denoted by C13.1. The following chart lists the algorithms of the server and client.

For simplicity, we assume that both the server and client run on the same computer. The server runs on the default localhost with IP = 127.0.0.1, and it uses a fixed port number 1234. This simplifies the program code. It also allows the reader to test run the server and client programs in different xterms on the same computer. The following shows the server and client program code using UDP.

/*  C13.1.a: UDP server.c file */

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/socket.h>

#include <netinet/ip.h>

#define BUFLEN 256    // max length of buffer

#define PORT 1234     // fixed server port number

char line[BUFLEN];

struct sockaddr_in me, client;

int sock, rlen, clen = sizeof(client);

int main()

{

printf(“1. create a UDP socket\n”);

sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

printf(“2. fill me with server address and port number\n”);

memset((char *)&me, 0, sizeof(me));

me.sin_family = AF_INET;

me.sin_port = htons(PORT);

me.sin_addr.s_addr = htonl(INADDR_ANY); // use localhost

printf(“3. bind socket to server IP and port\n”);

bind(sock, (struct sockaddr*)&me, sizeof(me));

printf(“4. wait for datagram\n”);

while(1){

memset(line, 0, BUFLEN);

printf(“UDP server: waiting for datagram\n”);

// recvfrom() gets client IP, port in sockaddr_in clinet

rlen=recvfrom(sock,line,BUFLEN,0,(struct sockaddr *)&client,&clen);

printf(“received a datagram from [host:port] = [%s:%d]\n”,

inet_ntoa(client.sin_addr), ntohs(client.sin_port));

printf(“rlen=%d: line=%s\n”, rlen, line);

printf(“send reply\n”);

sendto(sock, line, rlen, 0, (struct sockaddr*)&client, clen);

}

}

/***** C13.1.b: UDP client.c file *****/

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/socket.h>

#include <netinet/ip.h>

#define SERVER_HOST “127.0.0.1” // default server IP: localhost

#define SERVER_PORT 1234  // fixed server port number

#define BUFLEN        256       // max length of buffer

char line[BUFLEN];

struct sockaddr_in server;

int sock, rlen, slen=sizeof(server);

int main()

{

printf(“1. create a UDP socket\n”);

sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

printf(“2. fill in server address and port number\n”);

memset((char *) &server, 0, sizeof(server));

other.sin_family = AF_INET; other.sin_port = htons(SERVER_PORT);

inet_aton(SERVER_HOST, &server.sin_addr);

while(1){

printf(“Enter a line :   “);

fgets(line, BUFLEN, stdin);

line[strlen(line)-1] = 0;

printf(“send line to server\n”);

sendto(sock,line,strlen(line),0,(struct sockaddr *)&server,slen);

memset(line, 0, BUFLEN);

printf(“try to receive a line from server\n”);

rlen=recvfrom(sock,line,BUFLEN,0,(struct sockaddr*)&server,&slen);

printf(“rlen=%d: line=%s\n”, rlen, line);

}

}

Figure 13.7 shows the sample outputs of running the UDP server-client program C13.1.

Source: Wang K.C. (2018), Systems Programming in Unix/Linux, Springer; 1st ed. 2018 edition.

Leave a Reply

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