You are on page 1of 34

JIS College of Engineering

Department of Computer Science and Engineering


Paper Name: Computer Networks Lab
Paper Code: CS 691

Name Souvik Mondal


University Roll Number 123180703091
University Registration 181230110091
Number
Section B
Group G1
Mobile Number 7586884299
Email Id souvikmondalofficial@gmail.com
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691

Vision and Mission of the Department: -


Vision
The Computer Science and Engineering Department at JIS College of
Engineering will be a leader in computing innovation through excellence
in undergraduate and graduate education, active research programs and
the dissemination of knowledge.  The Department will leverage both the
international and interdisciplinary nature of computing. 

Mission
The Department's mission is
To provide students and faculty with an open environment that
encourages professional and personal growth.

To prepare students for flexible career paths and continuing advancement


in computing.

To motivate and encourage the students to build successful career in the


computing professions through flexible programme of study that can be
adapted to support individual career goals.

Department Program Educational Objectives


(PEOs)
The Program Educational Objectives (PEO) of the Mechanical Engineering Program
will demonstrate the essential components of a successful engineer for the best
career based professional accomplishments after graduation. Therefore the
objectives are following:
PEO 1: Graduates will be engineering practitioners and leaders, who
would assist to resolve industry's technological problems.
PEO 2: Graduates will be engineering professionals, innovators or
entrepreneurs engaged in technology development, technology
deployment, or engineering system implementation in industry
and research institute.
PEO 3: Graduates will interact with their peers in other disciplines in
industry and society and contribute to social awareness and the
economic growth of the country.
PEO 4: Graduates will be successful in pursuing higher studies in
engineering or management and will pursue career paths in
teaching or research.
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
Program Specific Outcomes (PSOs)
A graduate of the Computer Science and Engineering Program will demonstrate:

PSO1: Professional Skills: The ability to understand, analyse and develop


computer programs in the areas related to algorithms, system software,
multimedia, web design, big data analytics, and networking for efficient design of
computer-based systems of varying complexity.

PSO2: Problem-Solving Skills: The ability to apply standard practices and


strategies in software project development using open-ended programming
environments to deliver a quality product for business success.

PSO3: Successful Career and Entrepreneurship: The ability to employ modern


computer languages, environments, and platforms in creating innovative career
paths to be an entrepreneur, and a zest for higher studies and research.

Course Outcomes (COs):


CO1: Demonstrate the socket program using TCP & UDP.
CO2: Develop simple applications using TCP & UDP.
CO3: Develop the code for Data link layer protocol simulation.
CO4: Examine the performances of Routing protocol.
CO5: Experiment with congestion control algorithm using network simulator

Sl Name of the Experiment Date of Experiment Signature Marks


JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
1 Basic Networking and Different 17/2/21
type of network cables (Cross
Cable and Straight Cable) and
their structure.

2 Network Devices in Detail: 24/2/21


Repeater, Hub, Switch, Bridge,
Router, Gate Way

3 Transmission Media in Detail: 3/3/21


Twisted Pair Cable, Co-axial
cable, Optical Fibre

4 Router/Host IP configuration 10/3/21


5 Write a C program to implement 17/3/21
Single Parity Check
6 Write a C program to implement 24/3/21
Two-dimensional parity check.
7 Write a C++ program to 31/3/21
implement Cycle Redundancy
Check (CRC).

8 Write a C++ program to 7/4/21


implement Checksum
9 Network Commands (Linux 21/4/21
Environment).
10 Write a Client Server code to 5/5/21
fetch Date and Time using TCP
(Client Side)

11 Write a Client Server code to 12/5/21


fetch Date and Time using TCP
(Server Side)

12 Write a Client Server code to 19/5/21


Send Message using UDP

ASSIGNMENT 1:
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
Straight cable is a type of CAT5 with RJ-45 connectors at each end, and each has
the same pin out. It is in accordance with either the T568A or T568B standards.
It uses the same colour code throughout the LAN for consistency. This type of
twisted-pair cable is used in LAN to connect a computer or a network hub such
as a router. It is one of the most common types of network cable.

Here are applications where you should use Straight Cable:

 It helps you to connect a computer to a switch/hub's normal port.


 You can use it to connect a computer to a cable/DSL modem's LAN port.
 It allows you to connect a router's WAN port to a cable/DSL modem's LAN
port.
 Connect 2 switches or hubs with one of the hub or switches using an
upline port and the other one using a normal port.

A Crossover cable is a type of CAT 5 where one end isT568A configuration and
the other end as T568BConfiguration. In this type of cable connection, Pin 1 is
crossed with Pin 3, and Pin 2 is crossed with Pin 6.

Crossover cable is used to connect two or more computing devices. The internal
wiring of crossover cables reverses the transmission and receive signals. It is
widely used to connect two devices of the same type: e.g., two computers or two
switches to each other.
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691

Here is an application where you should use Crossover Cable:

 It can use a computer to a computer with no switch or hub.


 Network devise to the network device. For example, the route to the
router.
 Crossover cable enables one to establish a direct connection between two
computing devices using Ethernet ports.
 It Connects two computers directly.
 You can connect two hubs/switches by using the normal port in both
switches and hubs.

ASSIGNMENT 2:

Repeater – A repeater operates at the physical layer. Its job is to regenerate


the signal over the same network before the signal becomes too weak or
corrupted so as to extend the length to which the signal can be transmitted
over the same network. An important point to be noted about repeaters is that
they do not amplify the signal. When the signal becomes weak, they copy the
signal bit by bit and regenerate it at the original strength. It is a 2-port device. 

Hub – A hub is basically a multiport repeater. A hub connects multiple wires


coming from different branches, for example, the connector in star topology
which connects different stations. Hubs cannot filter data, so data packets are
sent to all connected devices.  In other words, collision domain of all hosts
connected through Hub remains one.  Also, they do not have the intelligence to
find out best path for data packets which leads to inefficiencies and wastage. 
 
Types of Hub 
Active Hub: - These are the hubs which have their own power supply and can
clean, boost, and relay the signal along with the network. It serves both as a
repeater as well as wiring centre. These are used to extend the maximum
distance between nodes.
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
 Passive Hub: - These are the hubs which collect wiring from nodes and
power supply from active hub. These hubs relay signals onto the
network without cleaning and boosting them and can’t be used to extend
the distance between nodes.
 Intelligent Hub: - It work like active hubs and include remote
management capabilities. They also provide flexible data rates to
network devices. It also enables an administrator to monitor the traffic
passing through the hub and to configure each port in the hub.

Switch – A switch is a multiport bridge with a buffer and a design that can
boost its efficiency (a large number of ports imply less traffic) and
performance. A switch is a data link layer device. The switch can perform error
checking before forwarding data, that makes it very efficient as it does not
forward packets that have errors and forward good packets selectively to
correct port only.  In other words, switch divides collision domain of hosts,
but broadcast domain remains same. 

Bridge – A bridge operates at data link layer. A bridge is a repeater, with add
on the functionality of filtering content by reading the MAC addresses of source
and destination. It is also used for interconnecting two LANs working on the
same protocol. It has a single input and single output port, thus making it a 2-
port device.
Types of Bridges:
 Transparent Bridges: - These are the bridge in which the stations are
completely unaware of the bridge’s existence i.e., whether or not a
bridge is added or deleted from the network, reconfiguration of the
stations is unnecessary. These bridges make use of two processes i.e.,
bridge forwarding and bridge learning.
 Source Routing Bridges: - In these bridges, routing operation is
performed by source station and the frame specifies which route to
follow. The host can discover frame by sending a special frame called
discovery frame, which spreads through the entire network using all
possible paths to destination.
  
Routers – A router is a device like a switch that routes data packets based on
their IP addresses. Router is mainly a Network Layer device. Routers normally
connect LANs and WANs together and have a dynamically updating routing
table based on which they make decisions on routing the data packets. Router
divide broadcast domains of hosts connected through it.

Gateway – A gateway, as the name suggests, is a passage to connect two


networks together that may work upon different networking models. They
basically work as the messenger agents that take data from one system,
interpret it, and transfer it to another system. Gateways are also called protocol
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
converters and can operate at any network layer. Gateways are generally more
complex than switch or router.

ASSIGNMENT 3:
Twisted Pair Cable:
Wires are twisted together in pairs. Each pair would consist of wire used for
the positive data signal and a wire used for the negative data signal. Any noise
that appears on positive/negative wire of the pair would occur on the other
wire. Because the wires are opposite polarities, these are 180 degrees out of
phase (180 degree or definition of opposite polarities). When the noise appears
on the both wires, it cancels or nulls itself out at the receiving used.
There are two types of twisted pair cable –
 Shielded Twisted Pair Cable –
Twisted pair cables are most effectively used in system that uses a
balanced line method of transmission. Cables with shield are called
Shielded twisted pair cables and commonly abbreviated STP.
 Unshielded Twisted Pair Cable –
Cables without shield are called unshielded twisted pair cables or UTP.
Twisting the wires together results in characteristics impedance for the
cable. UTP cable is used on Ethernet.
2. Co-axial Cable:
Co-axial cable consists of two conductors. The inner conductor is contained
inside the insulator with the other conductor weaves around it providing a
shield. An insulating protective coating called a jacket covers the outer
conductor. The outer shield protects the inner conductor from outside
electrical signals. Distance between the outer conductor and inner conductor
plus the type of material used for insulating the inner conductor determine the
cable properties.
3. Optical Fibre Cable:
Optical fibre consists of thin glass fibre that can carry information at
frequencies in the visible light spectrum. Typical optical fibre consists of a very
narrow strand of glass called the cladding. A typical core diameter is 62.5
microns.
Typically cladding has a diameter of 125 minors. Coating the cladding is a
protective coating consisting of plastic, it is called the jacket. The device
generating the message has it in electromagnetic from (electrical signal). This
has to be converted into light to send it on optical fibre cable.
ASSIGNMENT 4:
IP addresses are displayed as a set of four digits- the default address maybe
192.158.1.38. Each number on the set may range from 0 to 255. Therefore, the
total IP address range ranges from 0.0.0.0 to 255.255.255.255.
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
IP address is basically divided into two parts: X1. X2. X3. X4
1. [X1. X2. X3] is the Network ID
2. [X4] is the Host ID
 Network ID– 
It is the part of the left-hand IP address that identifies the specific
network where the device is located. In the normal home network,
where the device has an IP address 192.168.1.32, the 192.168.1 part of
the address will be the network ID. It is customary to fill in the last part
that is not zero, so we can say that the device’s network ID is
192.168.1.0.
 Hosting ID– 
The host ID is part of the IP address that was not taken by the network
ID. Identifies a specific device (in the TCP / IP world, we call devices
“host”) in that network. Continuing with our example of the IP address
192.168.1.32, the host ID will be 32- the unique host ID on the
192.168.1.0 network.

ASSIGNMENT 5:
#include <stdio.h>
#include <conio.h>
#include <string.h>
int calculation(int no)
{
int r,s=0;
while(no>0)
{
r = no%10;
s = s + r;
no = (no-r)/10;
}
return s;
}
void receiverParity(int choice)
{
int s,no;
printf("Enter Received Codeword : ");
scanf("%d",&no);
printf("\n");
s = calculation(no);
if(s%2==0 && choice==1 || s%2!=0 && choice==2)
printf("No error Occurred");
else
printf("Error Occurred");
}
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
void senderParity(int no)
{
int choice,s,codeword;
printf("1:Even Parity\n2: Odd Parity\n");
printf("\n");
printf("Enter Your Choice : ");
scanf("%d",&choice);
printf("\n");
s = calculation(no);
switch(choice)
{
case 1:
{
if(s%2!=0)
codeword = (no*10)+1;
else
codeword = no*10;
break;
}

case 2:
{
if(s%2!=0)
codeword = no*10;
else
codeword = (no*10)+1;
break;
}
default:
printf("Wrong Choice");
break;
}
printf("Codeword : %d\n\n",codeword);
receiverParity(choice);
}

int main()
{
printf("Simple Parity Checking\n");
printf("********************\n\n");
int no;
printf("Enter Binary Number : ");
scanf("%d",&no);
printf("\n");
senderParity(no);
}

OUTPUT:
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691

ASSIGNMENT 6:
#include <stdio.h>
#include <conio.h>

int rowCheck(int arr[][100], int i, int col)


{
int s=0;
int j=0;
while(j<col)
{
s = s + arr[i][j];
j++;
}
return s;
}
int colCheck(int arr[][100], int i, int row)
{
int s=0;
int j=0;
while(j<row)
{
s = s + arr[j][i];
j++;
}
return s;
}
void check(int code[][100], int arr[][100], int row, int col)
{
int i,j,errorRow=-1, errorCol=-1;
for(i=0;i<row;i++)
{
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
if(code[i][col]!=arr[i][col])
{
errorRow = i;
}
}
for(i=0;i<col;i++)
{
if(code[row][i]!=arr[row][i])
{
errorCol = i;
}
}
if(errorRow==-1 && errorCol==-1)
{
printf("No Error Occurred");
}
else
{
printf("\nError Position : (%d,%d)",errorRow, errorCol);
}
}
void receiver(int choice, int row, int col, int arr[][100])
{
int code[100][100],i,j,s;
printf("\n");
printf("Enter Received CodeWord : \n\n");
for(i=0;i<=row;i=i+1)
{
for(j=0;j<=col;j=j+1)
{
printf("code[%d][%d] : ",i,j);
scanf("%d",&code[i][j]);
}
printf("\n");
}
if(choice==1)
{
for(i=0;i<row;i=i+1)
{
s = rowCheck(code,i,col);
if(s%2!=0)
code[i][col] = 1;
else
code[i][col] = 0;
}
for(i=0;i<=col;i=i+1)
{
s = colCheck(code,i,row);
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
if(s%2!=0)
code[row][i] = 1;
else
code[row][i] = 0;
}
}
else
{
for(i=0;i<row;i=i+1)
{
s = rowCheck(code,i,col);
if(s%2!=0)
code[i][col] = 0;
else
code[i][col] = 1;
}
for(i=0;i<=col;i=i+1)
{
s = colCheck(code,i,row);
if(s%2!=0)
code[row][i] = 0;
else
code[row][i] = 1;
}
}
printf("Reciever Side Codeword : \n\n");
for(i=0;i<=row;i=i+1)
{
for(j=0;j<=col;j=j+1)
{
printf("%d\t",code[i][j]);
}
printf("\n");
}
check(code,arr,row,col);
}
void sender(int arr[][100], int choice,int row, int col)
{
int i,s,j;
if(choice==1)
{
for(i=0;i<row;i=i+1)
{
s = rowCheck(arr,i,col);
if(s%2!=0)
arr[i][col] = 1;
else
arr[i][col] = 0;
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
}
for(i=0;i<=col;i=i+1)
{
s = colCheck(arr,i,row);
if(s%2!=0)
arr[row][i] = 1;
else
arr[row][i] = 0;
}
}
else
{
for(i=0;i<row;i=i+1)
{
s = rowCheck(arr,i,col);
if(s%2!=0)
arr[i][col] = 0;
else
arr[i][col] = 1;
}
for(i=0;i<=col;i=i+1)
{
s = colCheck(arr,i,row);
if(s%2!=0)
arr[row][i] = 0;
else
arr[row][i] = 1;
}
}
printf("Sender Side Codeword : \n\n");
for(i=0;i<=row;i=i+1)
{
for(j=0;j<=col;j=j+1)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
receiver(choice,row,col,arr);
}

int main()
{
printf("Matrix Parity Checking\n");
printf("********\n\n");
int row,col;
printf("Enter Number of Rows : ");
scanf("%d",&row);
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
printf("Enter Number of Column : ");
scanf("%d",&col);
printf("\n");
int arr[100][100];
int i,j;
for(i=0;i<row;i=i+1)
{
for(j=0;j<col;j=j+1)
{
printf("arr[%d][%d] : ",i,j);
scanf("%d",&arr[i][j]);
}
printf("\n");
}
printf("Entered Matrix\n");
printf("******\n\n");
for(i=0;i<row;i=i+1)
{
for(j=0;j<col;j=j+1)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
int choice,s;
printf("\n");
printf("1: Even Parity\n2: Odd Parity\n\n");
printf("Enter Your Choice : ");
scanf("%d",&choice);
printf("\n");

sender(arr,choice,row,col);
}

OUTPUT:
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
ASSIGNMENT 7:
#include <iostream>
#include <sstream>
#include<bits/stdc++.h>
using namespace std;

string XOR(string a, string b)


{
string result = "";
int n = b.length();
for(int i = 1; i < n; i++)
{
if (a[i] == b[i])
result += "0";
else
result += "1";
}
return result;
}

string calculation(string dividend, string divisor)


{
int pick = divisor.length();
string tmp = dividend.substr(0, pick);
int n = dividend.length();

int i;
string divisor1="";
for(i=1;i<=pick;i++)
{
divisor1 = divisor1 + "0";
}

while (pick < n)


{
if (tmp[0] == '1')
tmp = XOR(divisor, tmp) + dividend[pick];
else
tmp = XOR(divisor1, tmp) + dividend[pick];

pick += 1;
}

if (tmp[0] == '1')
tmp = XOR(divisor, tmp) + dividend[pick];
else
tmp = XOR(divisor1, tmp) + dividend[pick];
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
return tmp;
}
void receiver(string key)
{
string r,tmp;
cout<<endl;
cout<<"Enter Receiver Side Codeword : ";
cin>>r;
cout<<endl;
tmp = calculation(r,key);
stringstream geek(tmp);
int x = 0;
geek >> x;
if(x == 0)
{
cout<<"Correct Codeword";
}
else
{
cout<<"Incorrect Codeword";
}
}
void encodeData(string data, string key)
{
int i,power,noOfZeroes=0;
string c = data;

for(i=0;i<=100;i=i+1)
{
power = pow(2,i);
if(power>=(i+data.length()+1))
{
noOfZeroes = i;
break;
}
}
for(i=1;i<=noOfZeroes;i++)
{
c = c + "0";
}

cout<<"Preliminary Codeword: "<<c<<endl<<endl;

string remainder = calculation(c, key);

string codeword = data + remainder;


JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
cout << "Encoded Dataword : "<< codeword << "\n";
}

int main()
{
cout<<"Cyclic Redundancy Check\n";
cout<<"***********************\n\n";

string data;
string key;

cout<<"Enter Dataword : ";


cin>>data;
cout<<"Enter Divisor : ";
cin>>key;
cout<<endl;

encodeData(data, key);
receiver(key);

return 0;
}

OUTPUT:

ASSIGNMENT 8:

#include <iostream>
#include <sstream>
#include<bits/stdc++.h>
using namespace std;
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
string xorCalculate(string a, string b)
{
string result = "";
int n = b.length();
for(int i = 1; i < n; i++)
{
if (a[i] == b[i])
result += "0";
else
result += "1";
}
return result;
}
int getMax(int *p)
{
int max=0,i;
for(i=0;i<5;i++)
{
if(max<*p)
{
max = *p;
}
p = p + 1;
}
return max;
}
string concatBinary(int *p, int c)
{
int i;
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
string j, s="";
for(i=0;i<c;i++)
{
stringstream ss;
ss<<*p;
ss>>j;
s = j + s;
p++;
}
return s;
}
string binaryConvert(int no)
{
int r,a[16],c=0,s=no;
string k;
while(no>0)
{
r = no%2;
a[c]=r;
no = no/2;
c=c+1;
}
k = concatBinary(a,c);
return k;
}
int countBit(string s)
{
int i=0,count=0;
while(s[i]!='\0')
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
{
count = count + 1;
i++;
}
return count;
}
int binaryToDeci(string no)
{
int p=0,s=0,i;
int l = no.length();
for(i=(l-1);i>=0;i--)
{
s = s + (((int)no[i]-48) * pow(2,p));
p = p + 1;
}
return s;
}
int onesComplement(string no)
{
string s="";
int l = no.length();
int i;
for(i=0;i<l;i=i+1)
{
if(no[i]=='0')
{
s = s + "1";
}
else
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
{
s = s + "0";
}
}
int result = binaryToDeci(s);
return result;
}
int main()
{
int n;
cout<<"Enter Number of Digit : ";
cin>>n;
cout<<endl;
int arr[n],max,i,sum=0,count0,count1,count2;
string k0,k1,result;
for(i=0;i<n;i=i+1)
{
cout<<"Enter "<<i+1<<" Number : ";
cin>>arr[i];
sum = sum + arr[i];
}
cout<<endl;
cout<<"Sum : "<<sum<<endl;
arr[n] = 0;
max = getMax(arr);

k0 = binaryConvert(sum);
//cout<<"Binary of "<<sum<<" : "<<k0<<endl;
count0 = countBit(k0);
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691

k1 = binaryConvert(max);
//cout<<"Binary of "<<max<<" : "<<k1<<endl;
count1 = countBit(k1);

int diff = count0 - count1;


string a = k0.substr(diff,count0);
string b = k0.substr(0,diff);
char zero = '0';
int d = diff;
while(diff>0)
{
b = zero + b;
diff--;
}
//cout<<b<<endl;
result = xorCalculate(a,b);
count2 = countBit(result);
diff=count1-count2;
while(diff>0)
{
result = zero + result;
diff--;
}
//cout<<result;
int wrapSum = binaryToDeci(result);
cout<<"Wrapped Sum : "<<wrapSum<<endl;
int checkSum = onesComplement(result);
cout<<"Check Sum : "<<checkSum;
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
}

OUTPUT:

ASSIGNMENT 9:
1. Ping (Packet Internet Groper)
ping command is used to ensure that a computer can communicate to a
specified device over the network. ping command sends Internet Control
Message Protocol (ICMP) Echo Request messages in the form of packets
to the destination computer and waits in order to get the response back.
Once the packets are received by the destined computer, it starts
sending the packets back. This command keeps executing until
it is interrupted.
ping command provides details such as
 number of packets transmitted
 number of packets received
 time taken by the packet to return
ping command in generally used for the following purposes:
 measuring the time taken by the packets to return to
determine speed of the connection
 to make sure that the network connection between host and
the destined computer can be established
2. nslookup
nslookup command queries the DNS in order to fetch the IP address or
the domain name from DNS records.
3. traceroute
This command is used to get the route of a packet. In other words,
traceroute command is used to determine the path along which a packet
travel. It also returns the number of hops taken by the packet to reach
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
the destination. This command prints to the console, a list of hosts
through which the packet travels in order to the destination.
4. host
host command is used to find domain name associated with the IP
address or find IP address associated with domain name. The returned
IP address is either IPv4 or IPv6.
5. netstat
netstat (Network Statistics) is the command that is used to display
routing table, connection information, the status of ports, etc. This
command works with Linux Network Subsystem. This command
basically displays the content of /proc/net file defined in the Linux file
system.
6. Arp
ARP (Address Resolution Protocol) command is used to display and
modify ARP cache, that contains the mapping of IP address to MAC
address. The system’s TCP/IP stack uses ARP in order to determine MAC
address associated with an IP address.
7. ifconfig
ifconfig (Interface Configuration) is a utility in operating system that is
used to set or display the IP address and netmask of a network interface.
It also provides commands to enable or disable an interface. Many UNIX-
like operating system initializes their network interfaces using ifconfig
at boot time. ifconfig is also used to view the MTU (Maximum
transmission unit).

ASSIGNMENT 10:
#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;
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
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);
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
// close the socket
close(sockfd);
}

Compilation –
Server side:
gcc server.c -o server
./server
Client side:
gcc client.c -o client
./client
Output –
Server side:
Socket successfully created..
Socket successfully binded..
Server listening..
server acccept the client...
From client: hi
To client : hello
From client: exit
To client : exit
Server Exit...
Client side:
Socket successfully created..
connected to the server..
Enter the string : hi
From Server : hello
Enter the string : exit
From Server : exit
Client Exit...

ASSIGNMENT 11:
#include <stdio.h>
#include <netdb.h>
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
#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

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;
}
}
}

int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;

// socket create and verification


JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
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


JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
close(sockfd);
}

Compilation –
Server side:
gcc server.c -o server
./server
Client side:
gcc client.c -o client
./client
Output –
Server side:
Socket successfully created..
Socket successfully binded..
Server listening..
server acccept the client...
From client: hi
To client : hello
From client: exit
To client : exit
Server Exit...
Client side:
Socket successfully created..
connected to the server..
Enter the string : hi
From Server : hello
Enter the string : exit
From Server : exit
Client Exit...

ASSIGNMENT 12:
#include <stdio.h>
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
#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

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;
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691
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;
}

Output :
$ ./server
Client : Hello from client
Hello message sent.
$ ./client
Hello message sent.
Server : Hello from server
JIS College of Engineering
Department of Computer Science and Engineering
Paper Name: Computer Networks Lab
Paper Code: CS 691

You might also like