PUNJAB ENGINEERING COLLEGE
(DEEMED TO BE UNIVERSITY)
COMPUTER NETWORKS
LAB FILE
SUBMITTED BY :
SUBMITTED TO :
Sehajdeep Singh Dr. Trilok
Chand
22103128 (HOD – CSE
Dept.)
CSE 2nd Year
PRACTICAL-A
A) Understanding and using of commands like ping, ipconfig,
hostname, getmac, arp, nslookup, netstat, tracert, route, pathping.
1. ping
2. hostname
3. getmac
4. ipconfig
5. arp
6. nslookup
7. netstat
netstat -f: The -f switch will force the netstat command to
display the Fully Qualified Domain Name (FQDN) for each
foreign IP addresses when possible.
netstat -p: Use the -p switch to show connections or statistics
only for a particular protocol. You can not define more than
one protocol at once, nor can you execute netstat with -p
without defining a protocol.
8. tracert
9. route
10. pathping
PRACTICAL-B
B) Socket Programming
1. Installing and learning Linux operating system – understanding the
basic commands to edit, compile and execute ‘C’ programs in Linux
environment.
2. Understanding the basics of sockets programming using ‘C’
Programming Language in Linux based environment :-
a) Creating and destroying sockets – socket() and close() functions.
b) Specifying addresses – sockaddr and sockaddr_in structures.
c) Understanding the syntax, purpose and use of various functions used in
sockets programing in ‘C’ programming language – connect(), send(),
recv(), bind(), listen(), accept(), sendto(), recvfrom() etc. functions.
3. TCP/IP based Echo Client program using ‘C’ Programming Language
in Linux based environment.
4. TCP/IP based Echo server program using ‘C’ Programming Language
in Linux based environment.
5. UDP/IP based Echo Client Program using ‘C’ Programming Language
in Linux based environment.
6. UDP/IP based Echo Server Program using ‘C’ Programming
Language in Linux based Environment.
Code: TCP SERVER
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Function designed for chat between client and server.
void func(int sockfd)
{
char buff[MAX];
int n;
// infinite loop for chat
for (;;) {
bzero(buff, MAX);
// read the message from client and copy it in buffer
read(sockfd, buff, sizeof(buff));
// print buffer which contains the client contents
printf("From client: %s\t To client : ", buff);
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')
;
// and send that buffer to client
write(sockfd, buff, sizeof(buff));
// if msg contains "Exit" then server exit and chat
ended.
if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}
}
}
// Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0)
{
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
}
else
printf("Server listening..\n");
len = sizeof(cli);
// Accept the data packet from client and verification
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
printf("server acccept failed...\n");
exit(0);
}
else
printf("server acccept the client...\n");
// Function for chatting between client and server
func(connfd);
// After chatting close the socket
close(sockfd);
}
TCP CLIENT
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}
int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
// connect the client socket to server socket
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) !=
0) {
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
// function for chat
func(sockfd);
// close the socket
close(sockfd);
}
UDP SERVER
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
{
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr,
sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
int len, n;
len = sizeof(cliaddr); //len is value/resuslt
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, ( struct sockaddr *)
&cliaddr,
&len);
buffer[n] = '\0';
printf("Client : %s\n", buffer);
sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
len);
printf("Hello message sent.\n");
return 0;
}
UDP CLIENT
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from client";
struct sockaddr_in servaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
{
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY;
int n, len;
sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *)
&servaddr,
sizeof(servaddr));
printf("Hello message sent.\n");
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, (struct sockaddr *)
&servaddr,
&len);
buffer[n] = '\0';
printf("Server : %s\n", buffer);
close(sockfd);
return 0;
}
PRACTICAL-C
C) Practicals based on Wireshark
1. Wireshark Lab: Getting Started – understanding the basics of
wireshark software – packet sniffer, packet analyzer and packet capture
library.
Wireshark Window:
APPLYING HTTP FILTER:
2. Wireshark lab: HTTP –
a) The basic GET/response interaction
b) The HTTP conditional GET/response interaction.
c)Retrieving large HTML files
d) Retrieving HTML files with embedded objects
e) HTTP authentication and security
.
3. Wireshark DNS
a) nslookup tool
b)ipconfig
c) Tracing DNS
d) tracing DNS using nslookup
4. Wireshark Lab – TCP - analyzing a trace of the TCP segments sent
and received in transferring a file from your computer to a remote
server, TCP’s use of sequence and acknowledgement numbers for
providing reliable data transfer, TCP’s congestion control algorithm,
TCP’s flow control mechanism
Alice.txt file
5. Wireshark Lab: IP – capturing packets from an execution of
traceroute, a look at the captured trace, fragmentation
PingPlotter:
Packet Size:2000 bytes
Packet Size:3500 Bytes
6.Wireshark Lab ICMP
a)ICMP messages generated by Ping
b) ICMP messages generated by Traceroute
7. Wireshark Lab: Ethernet and ARP
a) Capturing and analyzing Ethernet frames
b) braving ARP in action
8. Wireshark Lab: DHCP – examining the DHCP packets captured by a
host.
ipconfig/release
Ipconfig/renew
Ipconfig/renew
Ipconfig/release
Ipconfig/renew
DHCP
DHCP DISCOVER
DHCP OFFER
DHCP REQUEST
DHCP ACK