Computer Network Lab File
Computer Network Lab File
Computer Network Lab File
S.no. Topic
5. Write a c program why socket programming using UDP and TCP (e.g. simple DNS, data
and time client/server, echo client/server, iterative and concurrent server)
1. Write a code simulating ARP/RARP protocol in c.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DEVICES 10
#define MAX_IP_LENGTH 16
#define MAX_MAC_LENGTH 18
typedef struct {
char ip[MAX_IP_LENGTH];
char mac[MAX_MAC_LENGTH];
} Device;
Device devices[MAX_DEVICES];
int numDevices = 0;
int i;
if (strcmp(devices[i].ip, ip) == 0) {
return;
int i;
if (strcmp(devices[i].mac, mac) == 0) {
return;
int main() {
strcpy(devices[0].ip, "192.168.0.1");
strcpy(devices[0].mac, "00:11:22:33:44:55");
numDevices++;
strcpy(devices[1].ip, "192.168.0.2");
strcpy(devices[1].mac, "AA:BB:CC:DD:EE:FF");
numDevices++;
sendARPRequest("192.168.0.1");
return 0;
In this code, we define a Device structure that holds the IP address and MAC address of a device. We create an array
devices to store the simulated devices. The sendARPRequest function simulates sending an ARP request for a given
IP address and searches for a matching MAC address in the devices array. Similarly, the sendRARPRequest function
simulates sending a RARP request for a given MAC address and searches for a matching IP address in the devices
array.
In the main function, we add two devices to the simulation and then demonstrate sending an ARP request and a
RARP request. Note that this is a simplified simulation and doesn't include all the complexities of a real ARP/RARP
implementation.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#define PACKET_SIZE 64
#define MAX_HOPS 30
#define MAX_RETRIES 3
#define TIMEOUT 1
sum += *buffer++;
length -= 2;
if (length == 1) {
char packet[PACKET_SIZE];
memset(packet, 0, sizeof(packet));
icmp_hdr->icmp_type = ICMP_ECHO;
icmp_hdr->icmp_code = 0;
icmp_hdr->icmp_id = getpid();
icmp_hdr->icmp_seq = seq_num;
icmp_hdr->icmp_cksum = 0;
perror("sendto");
exit(1);
char buffer[PACKET_SIZE];
while (1) {
perror("recvfrom");
exit(1);
}
}
if (sockfd == -1) {
perror("socket");
exit(1);
dest_addr.sin_family = AF_INET;
dest_addr.sin_addr.s_addr = inet_addr(dest_ip);
int ttl;
gettimeofday(&start_time, NULL);
int retry;
gettimeofday(&end_time, NULL);
break;
if (retry == MAX_RETRIES) {
printf("%d\t*\n", ttl);
break;
close(sockfd);
if (argc != 2) {
return 1;
sendTracerouteRequest(dest_ip);
return 0;
In this code, we use sockets to send ICMP Echo Request packets and receive ICMP Echo Reply
packets to simulate the ping and traceroute commands.
The sendPingRequest function constructs and sends an ICMP Echo Request packet to the
destination IP address specified. The receivePingReply function receives ICMP Echo Reply packets
and checks if they match the expected sequence number.
The sendTracerouteRequest function sends a series of ICMP Echo Request packets with increasing
TTL (Time-to-Live) values to simulate the traceroute command. It sets the TTL for each packet,
sends it, and waits for the corresponding ICMP Echo Reply or TTL expired message. The elapsed
time is calculated for each successful reply and printed to the console.
In the main function, we specify the destination IP address as a command-line argument and call
the sendTracerouteRequest function to initiate the traceroute simulation.
This example focuses on a simple client-server architecture where the client makes a remote
procedure call to the server.
First, let's define the interface for the remote procedure. We'll use a basic arithmetic operation
as an example:
// rpc_interface.h
#ifndef RPC_INTERFACE_H
#define RPC_INTERFACE_H
Next, we'll implement the server code that provides the remote procedure:
// rpc_server.c
#include "rpc_interface.h"
#include <stdio.h>
return a + b;
int main() {
return 0;
Now, let's implement the client code that makes a remote procedure call to the server:
// rpc_client.c
#include "rpc_interface.h"
#include <stdio.h>
#include <rpc/rpc.h>
int main() {
CLIENT *cl;
if (cl == NULL) {
return 1;
int a = 5;
int b = 3;
if (result == -1) {
return 1;
return 0;
./server
./client
This is a simplified example of RPC in C using the RPC library. However, in real-world
scenarios, implementing a robust and secure RPC system requires additional considerations
such as serialization/deserialization, error handling, authentication, and transport protocols.
It's important to refer to the appropriate documentation and libraries for a complete and
production-ready RPC implementation.
To find the IP address, subnet mask, and default gateway in C, you can use system commands
to execute shell commands that retrieve this information from the operating system.
#include <stdio.h>
#include <stdlib.h>
#define MAX_COMMAND_LENGTH 100
int main() {
char command[MAX_COMMAND_LENGTH];
char result[MAX_COMMAND_LENGTH];
// Get IP address
if (ip_output == NULL) {
return 1;
pclose(ip_output);
if (subnet_output == NULL) {
return 1;
pclose(subnet_output);
if (gateway_output == NULL) {
return 1;
return 0;
In this code, we use the ipconfig command (for Windows) to retrieve the IP address, subnet
mask, and default gateway information. We pipe the output to grep and awk commands to
extract the specific values we need. The popen function allows us to execute the shell command
and read its output.
A C program that demonstrates socket programming using UDP and TCP for various purposes:
#define SERVER_PORT 53
#define MAX_BUFFER_SIZE 1024
int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
char buffer[MAX_BUFFER_SIZE];
if (sockfd < 0) {
perror("Socket creation failed");
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
while (1) {
socklen_t client_len = sizeof(client_addr);
ssize_t num_bytes = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&client_addr,
&client_len);
if (num_bytes < 0) {
perror("Error receiving data");
exit(1);
}
buffer[num_bytes] = '\0';
// Perform DNS lookup and obtain IP address based on the received domain name
// Send IP address to the client
sendto(sockfd, ip_address, strlen(ip_address), 0, (struct sockaddr*)&client_addr, client_len);
}
close(sockfd);
return 0;
}
// DNS Client
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
int main() {
int sockfd;
struct sockaddr_in server_addr;
char buffer[MAX_BUFFER_SIZE];
char domain_name[MAX_BUFFER_SIZE];
if (sockfd < 0) {
perror("Socket creation failed");
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
if (num_bytes < 0) {
perror("Error receiving data");
exit(1);
}
buffer[num_bytes] = '\0';
close(sockfd);
return 0;
}
The code provided above demonstrates a simple DNS client and server using UDP. To implement the other
examples (Date and Time, Echo, Iterative, and Concurrent), you can refer to the relevant resources and
adapt the code accordingly.