CN lab file SHIVAM
CN lab file SHIVAM
of
Bachelor of Technology
By
Shivam Sharma
2261528
Under the Guidance of
Mr. Anubhav Bewerwal
Assistant Professor
Department of CSE
The term work of Computer Networks Lab , being submitted by Shivam Sharma
S/O Mr. LD Sharma University Roll Number 2261528 to Graphic Era Hill
University, Bhimtal Campus for the award of bona fide work carried out by him.
She has worked under my guidance and supervision and fulfilled the requirement
Professor, Dept. of CSE, GEHU Bhimtal Campus) for allowing me to carry out this
practical work under his excellent and optimistic supervision. This has all been possible
due to his novel inspiration, able guidance and useful suggestions that have helped me in
I want to extend thanks to our President Prof. (Dr.) Kamal Ghanshala for providing us all
infrastructure and facilities to work in need without which this work would not be possible.
Shivam Sharma
University Roll Number: 2261528
INDEX
S No. Experiment Date Sign
1. Familiarization of Network Environment, Understanding and using
network utilities: ipconfig, netstat, ping, telnet, ftp, traceroute etc.
2. Familiarization with Transmission media and tools: Co-axial cable, UTP
cable, Crimping tool, Connectors etc. Preparing the UTP cable for cross
and direct connection using crimping tool.
3. Installation and introduction of simulation tool. (Packet Tracer)
4. To configure a basic network topology consisting of routers, switches, and
end devices such as PCs or laptops. Configure IP addresses and establish
connectivity between devices. (Using packet Tracer)
5. To configure a DHCP server on a router or a dedicated DHCP server
device. Assign IP addresses dynamically to devices on the network and
verify successful address assignment. (Using packet Tracer)
6. To configure a local DNS server to resolve domain names within a
network. (Using packet Tracer)
7. Network Troubleshooting: Simulate network issues such as connectivity
problems, incorrect configurations, or routing failures. Use Packet Tracer's
simulation mode to diagnose and troubleshoot the network.
8. NAT (Network Address Translation): Set up NAT on a router to
translate private IP addresses to public IP addresses for outbound
internet connectivity. Test the translation and examine how NAT
helps conserve IPv4 address space. (Using packet Tracer)
9.
TCP Client-Server Communication:
Implement a TCP client program that sends a message to a TCP
server program.
Implement the corresponding TCP server program that receives the
message and displays it.
Test the communication between the client and server by exchanging
messages
(Using ‘C’ Language)
10. UDP Client-Server Communication:
Implement a UDP client program that sends a message to a UDP
server program.
Implement the corresponding UDP server program that receives the
message and displays it (Using ‘C’ Language).
1. Familiarization of Network Environment, Understanding and using network
utilities: ipconfig, netstat, ping, telnet, ftp, traceroute etc.
3. ping (Packet Internet Groper) ping is a diagnostic tool used to test the reachability of a host on an
IP network. It sends ICMP echo request packets to the target host and measures the time taken for the
responses. It helps determine whether the destination device is online and how fast it responds.
Command Example:
ping google.com # Sends packets to Google's
servers ping 192.168.1.1 # Tests connectivity
with local router
5. netstat (Network Statistics) netstat is a command-line utility that displays active network
connections, listening ports, and network protocol statistics. It helps users monitor open
connections, detect suspicious activity, and troubleshoot network issues.
Command Examples:
netstat # Shows active connections
netstat -a # Shows all active ports and listening ports
6. telnet (Teletype Network Protocol) telnet is a network protocol and command-line tool used to
establish a text-based communication session with a remote host using the TCP/IP protocol. It is
often used to test connectivity to a specific port (like 80 for HTTP or 25 for SMTP), though it is
now mostly replaced by more secure alternatives like SSH.
Command Example:
telnet google.com 80 # Test connection to port 80 (HTTP)
7. ftp (File Transfer Protocol)
ftp is a standard network protocol used to transfer files between a client and a server over a TCP-based
network. The ftp command-line tool allows users to upload and download files, authenticate with remote
servers, and navigate remote directories.
Command Example:
ftp ftp.example.com
2. Familiarization with Transmission media and tools: Co-axial cable, UTP cable,
Crimping tool, Connectors etc. Preparing the UTP cable for cross and direct
connection using crimping tool.
3. Crimping Tool
A crimping tool is a hand tool used to attach connectors (such as RJ-45) to the ends of UTP cables. It
ensures a secure and reliable electrical connection by pressing the connector’s pins into the cable wires.
Function: Terminating cables with RJ-45 connectors
4. Connectors (RJ-45)
RJ-45 connectors are modular plugs used to connect UTP cables to networking devices. They have 8 pins
that correspond to the 8 wires in a UTP cable. These connectors are crucial in forming both straight-
through and crossover cables.
Pin Count: 8P8C (8 Positions, 8 Contacts)
Use: Ethernet, LAN connections
Preparing UTP Cable for Cross and Direct Connection
configure terminal
Step 5: Verify
On each PC, after clicking DHCP:
• It should auto-fill with an IP like 192.168.10.100+
• Subnet Mask: 255.255.255.0
• Gateway: 192.168.10.1 Now test it: ping 192.168.10.1 # router ping 192.168.10.101 #
other PC
6. To configure a local DNS server to resolve domain names within a network. (Using
packet Tracer)
Step 1: Connections
Use straight-through cables:
• PC0, PC1 → Switch
• DNS Server → Switch
• Switch → Router (Fa0/0)
Objective:
Set up NAT on a router to translate private IPs to public IPs.
Test the translation using ping.
Understand how NAT helps conserve IPv4 addresses.
Tools Required:
A C compiler (e.g., GCC)
Run on Linux/Unix or Windows (using MinGW or Code::Blocks)
// File: tcp_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // for close()
#include <netinet/in.h> // for sockaddr_in
int main() {
int server_fd, new_socket;
char buffer[BUFFER_SIZE] = {0};
struct sockaddr_in address;
int addrlen = sizeof(address);
// 1. Create socket
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}
// 3. Listen
if (listen(server_fd, 3) < 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
// 4. Accept connection
new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
if (new_socket < 0) {
perror("Accept failed");
exit(EXIT_FAILURE);
}
// 6. Close sockets
close(new_socket);
close(server_fd);
return 0;
}
// File: tcp_client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // for close()
#include <arpa/inet.h> // for sockaddr_in, inet_addr
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
char *message = "Hello from client!";
char buffer[BUFFER_SIZE] = {0};
// 1. Create socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
printf("\n Socket creation error \n");
return -1;
}
// 2. Set server address
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 addresses from text to binary
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/Address not supported \n");
return -1;
}
// 3. Connect to server
if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}
// 4. Send message to server
send(sock, message, strlen(message), 0);
printf("Message sent to server: %s\n", message);
// 5. Read server reply
read(sock, buffer, BUFFER_SIZE);
printf("Message from server: %s\n", buffer);
// 6. Close socket
close(sock);
return 0;
}
On Linux Terminal:
gcc tcp_server.c -o server
gcc tcp_client.c -o client
Run in two separate terminals:
Terminal 1 (Start Server):
./server
Terminal 2 (Start Client):
./client
Output Example
Server:
Server is listening on port 8080...
Message from client: Hello from client!
Client:
Message sent to server: Hello from client!
Message from server: Hello from server!
10. UDP Client-Server Communication:
Implement a UDP client program that sends a message to a UDP server
program.
Implement the corresponding UDP server program that receives the
message and displays it
(Using ‘C’ Language)
Objective:
// File: udp_server.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
int main() {
int sockfd;
char buffer[BUFFER_SIZE];
struct sockaddr_in servaddr, cliaddr;
// 1. Create socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// 4. Receive message
int len, n;
len = sizeof(cliaddr);
n = recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr *) &cliaddr, &len);
buffer[n] = '\0';
// 5. Display message
printf("Message from client: %s\n", buffer);
// 6. Close socket
close(sockfd);
return 0;
}
// File: udp_client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
int main() {
int sockfd;
char buffer[BUFFER_SIZE];
char *message = "Hello from client!";
struct sockaddr_in servaddr;
// 1. Create socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// 5. Close socket
close(sockfd);
return 0;
}
Terminal 1:
./udp_server
Terminal 2:
./udp_client
Output Example
Server Output:
UDP Server is running on port 8080...
Message from client: Hello from client!
Client Output:
Message sent to server: Hello from client!
Message from server: Message received!