0% found this document useful (0 votes)
53 views4 pages

Experiment-5 NT-LAB Manual

The document describes an experiment involving developing UDP client and server programs in C using socket programming APIs. It includes the code for the client and server programs and steps to compile and run them on one or two Linux machines, and observe the message exchange using the terminal and packet capture using Wireshark.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views4 pages

Experiment-5 NT-LAB Manual

The document describes an experiment involving developing UDP client and server programs in C using socket programming APIs. It includes the code for the client and server programs and steps to compile and run them on one or two Linux machines, and observe the message exchange using the terminal and packet capture using Wireshark.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Networks Lab

Experiment-5
Aim of the Experiment: Development of client-side and server-side socket programs for the
connection-less User Datagram Protocol (UDP) process-to-process communication using ‘c’language
using UNIX Socket Programming APIs. Execute the client/server programs and analyze the
message/Protocol Data unit (PDU) exchange between client-server using Wireshark Protocol Analyzer.

Objective: Students will get hands-on experience on UNIX Socket Application Programming
Interfaces (APIs) and analyze the traffic /protocols using Wireshark Protocol Analyzer for UDP
client/server communications.

Software Required: UNIX/Linux OS with GNU Compiler Collection (gcc) complier, Wireshark
Protocol Analyzer

Network Scenario: In a network, two computers/hosts need to communicate over a connection- less
framework over UDP. To enable this communication two socket programs has to be written in‘c’-
language. The server program is ‘udp_server.c’ and the client program is ‘udp_client.c’. After writing
the code, it has to be complied using UNIX gcc compiler. We have to considered two machines running
over Linux OS for implementation. The message communication between client and server output to
be visualized in Linux ‘Terminal’ window. Additionally, the protocol data units / packets exchanged
between client and server need to be analyzed using Wireshark protocol analyzer.
Fundamentals of Client / Server Communication using Socket Programming

Fig. 1 Connection-lessUDP Client/server communication


udp_server.c
/*** A datagram sockets "server" demo */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MYPORT 4952 // the port users will be connecting to
#define MAXBUFLEN 200

int main()
{
int sockfd;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
socklen_t addr_len;
int numbytes;
char buf[MAXBUFLEN];

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)


{perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
//memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1)


{perror("bind");
exit(1);
}

addr_len = sizeof their_addr;


if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1)
{ perror("recvfrom");
exit(1);
}
printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n",numbytes);
buf[numbytes] = '\0';
printf("packet contains \"%s\"\n",buf);
close(sockfd);
return 0;
}
udp_client.c

/*** A datagram "client" demo */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define SERVERPORT 4952 // the port users will be connecting to

int main()
{
int sockfd;
struct sockaddr_in their_addr; // connector's address information
//struct hostent *he;
int numbytes;
char arg[30];

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)


{perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(SERVERPORT); // short, network byte order
their_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Insert UDP Server IP Address
//memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);

printf("Enter a message\n");
gets(arg);

if ((numbytes = sendto(sockfd, arg, strlen(arg), 0,


(struct sockaddr *)&their_addr, sizeof their_addr)) == -1)
{perror("sendto");
exit(1);
}
printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
close(sockfd);
return 0;
}
Procedure:

 Write the programs using any text editor


 Open Linux ‘Terminal’
 Navigate to PATH: cd /root/NT-LAB/EXPT7
 Compile the programs in Linux ‘Terminal’
o Command: gcc <filename.c> -o <filename of Executable>
 As an e.g.,  gcc udp_server.c -o udp_server
 As an e.g.,  gcc udp_client.c -o udp_client

 Execute the program in Linux ‘Terminal’:


o Command: ./ <filename of Executable>
o As an e.g.  ./ udp_server and ./udp_client (In separate Terminal windows)

 Run the client and server with in the same host.


 Run the client in one host and the server in another host.
 Observations : Record Observations

Observations: (Take Screen-shorts for putting Outputs in the Record)

A. Terminal
 Observe the messages transaction between client-server and vice-versa.

B. Wireshark Protocol Analyzer


 Finally capture and analyze the packets exchanged between them. (using Wireshark)
 Open “Wireshark’
 Capture “Loopback:lo”(Same Host) or“enpos3” (Different hosts) Network Interface
 Filter “udp”
 Analyze > Follow > UDP Stream (UDP Payload/ Data)
 Statistics > I/O Graphs (Study ‘udp’ filtered packets), Statistics >Flow Graph

Conclusion: Students should write the final outcome of this experiment in their own language.

***

You might also like