Computer Networks Lab Manual
Computer Networks Lab Manual
in
IT6511
NETWORKS LABORATORY
LTPC
0 0 3 2
OBJECTIVES:
The student should be made to:
Learn socket programming.
Be familiar with simulation tools.
Have hands on experience on various networking protocols.
LIST OF EXPERIMENTS:
1. Implementation of Stop and Wait Protocol and Sliding Window Protocol.
2. Study of Socket Programming and Client Server model
3. Write a code simulating ARP /RARP protocols.
4. Write a code simulating PING and TRACEROUTE commands
5. Create a socket for HTTP for web page upload and download.
6. Write a program to implement RPC (Remote Procedure Call)
7. Implementation of Subnetting .
8. Applications using TCP Sockets like
a. Echo client and echo server
b. Chat
c. File Transfer
9. Applications using TCP and UDP Sockets like
d. DNS
e. SNMP
f. File Transfer
10. Study of Network simulator (NS).and Simulation of Congestion Control Algorithms
using NS
11. Perform a case study about the different routing algorithms to select the network path
with its optimum and economical during data transfer.
i. Link State routing
ii. Flooding
iii. Distance vector
TOTAL: 45 PERIODS
Staff In-Charge
HOD/IT
Branch: IT
Year/Sem/Sec: III/V/A
Ex.No
Date
01-07-15
08-07-15
15-07-15
22-07-15
29-07-15
05-08-15
08-08-15
12-08-15
19-08-15
10
26-08-15
11
02-09-15
12
09-09-15
13
16-09-15
Revision - 1
14
23-09-15
Revision - 2
15
30-09-15
16
12-10-15
Staff In-charge
HOD/IT
Year/Sem/Sec: III/V/B
Ex.No
Date
03-07-15
10-07-15
17-07-15
24-07-15
31-07-15
07-08-15
14-08-15
21-08-15
28-08-15
10
04-09-15
11
11-09-15
12
12-09-15
13
18-09-15
Revision - 1
14
25-09-15
Revision - 2
15
09-10-15
16
12-10-15
Staff In-charge
Ex.No.1a
HOD/IT
AIM:
To study about socket programming for developing the client server application.
DESCRIPTION
A socket is an endpoint for communication. Two processes can communicate by creating
sockets and sending messages between them. A socket type is uniquely determined by a
<domain, type, protocol> triple.
a= socket(addressfamily,type of socket,protocol)
addressfamily=AF_INET type of socket=SOCK_STREAM (or)
SOCK_DGRAM protocol= 0 (for system default protocol)
a=unique descriptor
In order to hold information about the socket, socket address structures are used.
int bind (socket descriptor, socket address structure,
sizeof(socketaddressstructure)) return 0 if OK,-1 on error
int listen( int sockfd, int backlog)
return 0 if OK,-1 on error
int accept(int sockfd, void *addr, void *addrlen);
return positive value if OK, -1 on error
int connect(int sockfd, socket address structure, sizeof(socketaddressstructure))
RESULT:
Thus the socket programming was studied.
Exp. No.1b
SOCKET CREATION
AIM
To write a C program for creating a socket
ALGORITHM
1.
2.
Declare 2 variables as TCP and UDP file descriptors for holding the values of
TCP and UDP socket.
3.
Call the System function Socket ( ) with the following parameters type of family, type
of socket and type of protocol for creating the socket.
4.
If the System function returns negative value then the print error message else
print socket created with socket value.
Exp. No.1c
SOCKET BINDING
AIM
To write a C program for socket binding
ALGORITHM
1. Include the necessary header files.
2. Declare 2 variables as TCP and UDP file descriptors for holding the values of
3. TCP and UDP socket.
4. Call the System function socket() with the following parameters type of family, type
of socket and type of protocol for creating the socket.
5. Call the system function bind() with the following parameters socket descriptor,
socket address structure and size of socket address structure.
RESULT:
Thus the socket creation and socket binding was performed.
Exp. No.2
Aim:
To write a C program for client server chat using TCP
Theory
TCP
TCP stands for Transaction Control Protocol. This is one of the transport layer protocols. TCP offers a
connection-oriented, reliable, byte stream service. This also provides a good flow control and
congestion control. The term connection-oriented means any two applications using TCP must
establish a connection with each other before they can exchange data. This can be done by using
three-way handshaking.
Data is divided into small chunks and each chunk of data is called as Segments. The data sent
using TCP follows a single route and it can be in out-of-order. The receiver will rearrange the data
using the sequence numbers.
Flow control is established by using a finite amount of buffer space on both sides. With this
the receiver allows the sender to send only the amount of data it can accept in the buffer. TCP also
provides error control. A checksum is maintained in the header and data.
Program
Serverchat
import java.net.*;
import java.io.*;
public class Serverchat{
public static void main(String[]args)throws IOException{
ServerSocket sock=null;
BufferedReader fromClient=null,fromUser=null;
PrintWriter toClient=null;
Socket client=null;
try{
sock=new ServerSocket(4444);
System.out.println("Server Ready");
client=sock.accept();
System.out.println("Client connected");
fromClient=new BufferedReader(new
InputStreamReader(client.getInputStream()));
toClient=new PrintWriter(client.getOutputStream());
fromUser=new BufferedReader(new InputStreamReader(System.in));
String line,Usermsg;
while(true){
line=fromClient.readLine();
if((line==null) || (line.equals("bye")))
break;
System.out.println("Client:" +line);
System.out.print("me:");
Usermsg=fromUser.readLine();
toClient.println(Usermsg);
toClient.flush();
}
fromClient.close();
toClient.close();
client.close();
System.out.println("Client disconnected");
}
catch(IOException ioe){
System.out.println(ioe);
}
}
}
Clientchat
import java.net.*;
import java.io.*;
public class Clientchat{
public static void main(String[]args)throws IOException{
BufferedReader fromServer=null,fromUser=null;
PrintWriter toServer=null;
Socket sock=null;
try{
if(args.length==0)
sock=new Socket(InetAddress.getLocalHost(),4444);
else
sock=new Socket(InetAddress.getByName(args[0]),4444);
fromServer=new BufferedReader(new
InputStreamReader(sock.getInputStream()));
fromUser=new BufferedReader(new InputStreamReader(System.in));
toServer=new PrintWriter(sock.getOutputStream(),true);
String Usermsg,Servmsg;
System.out.println("Type \"bye\" to quit");
while(true){
System.out.print("me:");
Usermsg=fromUser.readLine();
if(Usermsg==null||Usermsg.equals("bye")){
toServer.println("bye");
break;
}
else
toServer.println(Usermsg);
Servmsg=fromServer.readLine();
System.out.println("Server:" +Servmsg);
}
fromUser.close();
fromServer.close();
toServer.close();
sock.close();
}
catch(IOException ioe){
System.err.println(ioe);
}
}
}
Result
Thus the C program for client-server chat using TCP is written, entered, executed and the
results are verified.
EX.NO:3
AIM:
To implementation of echo client server using TCP/IP
ALGORITHM:
1.start the program
2 To create a socket in client to server.
3. 3he client establishes a connection to the server.
4. The client accept the connection and send data to server and the server to replay the
echo message to the client
5. The client communicate the server to send the end of the message
6. Stop the program.
Program
Server
import java.net.*;
import java.io.*;
public class Serverecho{
public static void main(String[]args)throws IOException{
ServerSocket sock=null;
BufferedReader fromClient=null;
OutputStreamWriter toClient=null;
Socket client=null;
try{
sock=new ServerSocket(4444);
System.out.println("Server Ready");
client=sock.accept();
System.out.println("Client connected");
fromClient=new BufferedReader(new
InputStreamReader(client.getInputStream()));
toClient=new OutputStreamWriter(client.getOutputStream());
String line;
while(true){
line=fromClient.readLine();
if((line==null)||(line.equals("bye")))
break;
System.out.println("client[" +line+ "]\n");
toClient.write("server[" +line+ "]\n");
toClient.flush();
}
fromClient.close();
toClient.close();
client.close();
System.out.println("Client disconnected");
}
catch(IOException ioe){
System.out.println(ioe);
}
}}
Client
import java.net.*;
import java.io.*;
public class Clientecho{
public static void main(String[]args)throws IOException{
BufferedReader fromServer=null,fromUser=null;
PrintWriter toServer=null;
Socket sock=null;
try{
if(args.length==0)
sock=new Socket(InetAddress.getLocalHost(),4444);
else
sock=new Socket(InetAddress.getByName(args[0]),4444);
fromServer=new BufferedReader(new
InputStreamReader(sock.getInputStream()));
fromUser=new BufferedReader(new InputStreamReader(System.in));
toServer=new PrintWriter(sock.getOutputStream(),true);
String Usermsg,Servmsg;
System.out.println("Type \"bye\" to quit");
while(true){
System.out.println("Enter msg to server:");
Usermsg=fromUser.readLine();
if(Usermsg.equals("bye")){
toServer.println("bye");
break;
}
else
toServer.println(Usermsg);
Servmsg=fromServer.readLine();
System.out.println(Servmsg);
}
fromUser.close();
fromServer.close();
toServer.close();
sock.close();
}
catch(IOException ioe){
System.err.println(ioe);
}
}
}
Result
Thus the echo client server using TCP/IP was implemented successfully.
Ex.No : 4
Aim
To write a java program for applaction using TCP Sockets
Algorithm
1.Start the program.
2.Get the frame size from the user
3.To create the frame based on the user request.
4.To send frames to server from the client side.
5.If your frames reach the server it will send ACK signal to client otherwise it will
send NACK signal to client.
6.Stop the program
Program
Clientfile
import java.net.*;
import java.io.*;
import java.lang.String;
public class ClientFile{
public static void main(String [] args)throws IOException{
int i;
//String s="ServerFile.txt";
InputStream is=null;
BufferedOutputStream bos=null;
Socket sock=null;
try{
if(args.length==0)
sock=new Socket(InetAddress.getLocalHost(),4000);
else
sock=new Socket(InetAddress.getByName(args[0]),4000);
System.out.println("Connecting...");
String s1="clientFile.txt";
File myfile = new File(s1);
byte [] arrayy=new byte[1500];
is=sock.getInputStream();
bos=new BufferedOutputStream(new FileOutputStream(myfile));
i=is.read(arrayy,0,arrayy.length);
bos.write(arrayy,0,i);
bos.flush();
}
catch(IOException ioe){
System.out.println(ioe);
}
}
}
Serverfile
import java.net.*;
import java.io.*;
import java.lang.String;
public class ServerFile{
public static void main(String [] args)throws IOException{
ServerSocket sock=null;
BufferedInputStream bis=null;
Socket client=null;
OutputStream os=null;
int a;
try{
sock=new ServerSocket(4000);
System.out.println("Waiting...");
client=sock.accept();
System.out.println("Server Ready");
System.out.println("Accepted connection:" +client);
String s="ServerFile.txt";
File myfilee=new File(s);
byte [] array=new byte[(int)myfilee.length()];
bis=new BufferedInputStream(new FileInputStream(myfilee));
a=bis.read(array,0,array.length);
os=client.getOutputStream();
os.write(array,0,a);
os.flush();
}
catch(IOException ioe){
System.out.println(ioe);
}
}
}
Result
Thus the file transfer using TCP sockets was executed successfully.
EX-NO. 5
Aim:
To Write The java program for simulating ping and traceroute commands
ALGORITHM:
1.Start the program.
2.Get the frame size from the user
3.To create the frame based on the user request.
4.To send frames to server from the client side.
5.If your frames reach the server it will send ACK signal to client otherwise it will
send NACK signal to client.
6.Stop the program
Program
//pingclient.java
import java.io.*;
import java.net.*;
import java.util.Calendar;
class pingclient
{
public static void main(String args[])throws Exception
{
String str;
int c=0;
long t1,t2;
Socket s=new Socket("127.0.0.1",5555);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream out=new PrintStream(s.getOutputStream());
while(c<4)
{
t1=System.currentTimeMillis();
str="Welcome to network programming world";
out.println(str);
System.out.println(dis.readLine());
t2=System.currentTimeMillis();
System.out.println(";TTL="+(t2-t1)+"ms");
c++;
}
s.close();
}
}
//pingserver.java
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
class pingserver
{
public static void main(String args[])throws Exception
{
ServerSocket ss=new ServerSocket(5555);
Socket s=ss.accept();
int c=0;
while(c<4)
{
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream out=new PrintStream(s.getOutputStream());
String str=dis.readLine();
out.println("Reply from"+InetAddress.getLocalHost()+";Length"+str.length());
c++;
}
s.close();
}
}
Result:
Thus the program was implementing to simulating ping and traceroute commands
Ex.No:7
Aim:
To write a java program to implement RPC (remote procedure call
Algorithm :
1.Start the program.
2.Get the frame size from the user
3.To create the frame based on the user request.
4.To send frames to server from the client side.
5.If your frames reach the server it will send ACK signal to client otherwise it will
send NACK signal to client.
6.Stop the program
Program:
RPC PROGRAM
Client
import java.io.*;
import java.net.*;
import java.util.*;
class Clientrpc
{
public static void main(String args[])
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Socket clsct=new Socket("127.0.0.1",139);
DataInputStream din=new DataInputStream(clsct.getInputStream());
DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
System.out.println("Enter String");
String str=in.readLine();
dout.writeBytes(str+'\n');
clsct.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Server
import java.io.*;
import java.net.*;
import java.util.*;
class Serverrpc
{
public static void main(String args[])
{
try
{
ServerSocket obj=new ServerSocket(139);
while(true)
{
Socket obj1=obj.accept();
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
String str=din.readLine();
Process p=Runtime.getRuntime().exec(str);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT
Server
Y:\networks\remote>java Serverrpc
Client
Y:\networks\remote>java Clientrpc
Enter String
Calc
Result :
Thus the program was implementing to implement RPC (remote procedure call)
System.out.print("Acknowledgment received");
ano=Integer.parseInt(in1.readLine());
System.out.println(" for "+ano+" frames");
sws+=nf;
}
else
{
System.out.println("The no. of frames exceeds window size");
break;
}
System.out.print("\nDo you wants to send some more frames : ");
ch=in.readLine(); p.println(ch);
}
while(ch.equals("yes"));
s.close();
}
}
RECEIVER PROGRAM
import java.net.*;
import java.io.*;
class slidreceiver
{
public static void main(String a[])throws Exception
{
Socket s=new Socket(InetAddress.getLocalHost(),10);
DataInputStream in=new DataInputStream(s.getInputStream());
PrintStream p=new PrintStream(s.getOutputStream());
int i=0,rptr=-1,nf,rws=8;
String rbuf[]=new String[8];
String ch; System.out.println();
do
{
nf=Integer.parseInt(in.readLine());
if(nf<=rws-1)
{
For(i=1;i<=nf;i++)
{
rptr=++rptr%8;
rbuf[rptr]=in.readLine();
System.out.println("The received Frame " +rptr+" is : "+rbuf[rptr]);
}
rws-=nf;
System.out.println("\nAcknowledgment sent\n");
p.println(rptr+1); rws+=nf; }
else
break;
ch=in.readLine();
}
while(ch.equals("yes"));
}
}
OUTPUT:
//SENDER OUTPUT
Enter the no. of frames : 4
Enter 4 Messages to be send
hiii
how r u
i am fine
how is evryone
Acknowledgment received for 4 frames
Do you wants to send some more frames : no
//RECEIVER OUTPUT
The received Frame 0 is : hiii
The received Frame 1 is : how r u
The received Frame 2 is : i am fine
The received Frame 3 is : how is everyone
Result
Thus the program was implemented successfully.
Ex.No:9
Aim:
To write a java program for simulating arp/rarp protocols
ALGORITHM:
server
1. Create a server socket and bind it to port.
2. Listen for new connection and when a connection arrives, accept it.
3. Send servers date and time to the client.
4. Read clients IP address sent by the client.
5. Display the client details.
6. Repeat steps 2-5 until the server is terminated.
7. Close all streams.
8. Close the server socket.
9. Stop.
Client
1. Create a client socket and connect it to the servers port number.
2. Retrieve its own IP address using built-in function.
3. Send its address to the server.
4. Display the date & time sent by the server.
5. Close the input and output streams.
6. Close the client socket.
7. Stop.
Program
Program for Address Resolutuion Protocol (ARP) using TCP
Client:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientarp
{
public static void main(String args[])
{
Try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Socket clsct=new Socket("127.0.0.1",139);
DataInputStream din=new DataInputStream(clsct.getInputStream());
DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
System.out.println("Enter the Logical address(IP):");
String str1=in.readLine();
dout.writeBytes(str1+'\n');
String str=din.readLine();
System.out.println("The Physical Address is: "+str);
clsct.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverarp
{
public static void main(String args[])
{
try
{
ServerSocket obj=new ServerSocket(139);
Socket obj1=obj.accept();
while(true)
{
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
String str=din.readLine();
String ip[]={"165.165.80.80","165.165.79.1"};
String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
for(int i=0;i<ip.length;i++)
{
if(str.equals(ip[i]))
{
dout.writeBytes(mac[i]+'\n');
break;
}
}
obj.close();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
E:\networks>java Serverarp
E:\networks>java Clientarp
Enter the Logical address(IP):
165.165.80.80
The Physical Address is: 6A:08:AA:C2
while(true)
{
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
server.receive(receiver);
String str=new String(receiver.getData());
String s=str.trim();
//System.out.println(s);
InetAddress addr=receiver.getAddress();
int port=receiver.getPort();
String ip[]={"165.165.80.80","165.165.79.1"};
String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
for(int i=0;i<ip.length;i++)
{
if(s.equals(mac[i]))
{
sendbyte=ip[i].getBytes();
DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
server.send(sender);
break;
}
}
break;
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
I:\ex>java Serverrarp12
I:\ex>java Clientrarp12
Enter the Physical address (MAC):
6A:08:AA:C2
The Logical Address is(IP): 165.165.80.80
Result :
Thus the program for implementing to display simulating ARP /RARP protocols.
Ex.No:10
Aim:
To write a java program for socket for HTTP for web page upload and download .
Algorithm
1.Start the program.
2.Get the frame size from the user
3.To create the frame based on the user request.
4.To send frames to server from the client side.
5.If your frames reach the server it will send ACK signal to client otherwise it will
send NACK signal to client.
6.Stop the program
Program :
Client
import javax.swing.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Client{
public static void main(String args[]) throws Exception{
Socket soc;
BufferedImage img = null;
soc=new Socket("localhost",4000);
System.out.println("Client is running. ");
try {
System.out.println("Reading image from disk. ");
img = ImageIO.read(new File("digital_image_processing.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", baos);
baos.flush();
l.setIcon(icon);
f.add(l);
f.pack();
f.setVisible(true);
}
}
Download
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class Download {
public static void main(String[] args) throws Exception {
try{
String fileName = "digital_image_processing.jpg";
String website = "http://tutorialspoint.com/java_dip/images/"+fileName;
System.out.println("Downloading File From: " + website);
URL url = new URL(website);
InputStream inputStream = url.openStream();
OutputStream outputStream = new FileOutputStream(fileName);
byte[] buffer = new byte[2048];
int length = 0;
while ((length = inputStream.read(buffer)) != -1) {
System.out.println("Buffer Read of length: " + length);
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}catch(Exception e){
System.out.println("Exception: " + e.getMessage());
}
}
}
Output
When you run the client code, following output screen would appear on client side.
Result
Thus the web page was uploaded and downloaded successfully using HTTP Sockets.
Exp. No.11
Network Simulator:
A network simulator is a piece of software or hardware that predicts the
behavior of a network, without an actual network being present.
Uses of network simulators:
1. Network simulators serve a variety of needs. Compared to the cost and time involved in
setting up an entire test bed containing multiple networked computers, routers and data links.
Network simulators are relatively fast and inexpensive.
2. They allow engineers to test scenarios that might be particularly difficult or expensive
to emulate using real hardware. For an instance, simulating the effects of a sudden burst in
traffic or a DoS attack on a network service.
3. Networking simulators are particularly useful in allowing designers to test new
networking protocols or changes to existing protocols in a controlled and reproducible
environment.
4. Network simulators, as the name suggests are used by researchers, developers and QA to
design various kinds of networks, simulate and then analyze the effect of various parameters
on the network performance.
5.A typical network simulator encompasses a wide range of networking technologies
and help the users to build complex networks from basic building blocks like variety of nodes
and links.
6. With the help of simulators one can design hierarchical networks using various types
of nodes like computers, hubs, bridges, routers, switches, links, mobile units etc.
7. There are a wide variety of network simulators, ranging from the very simple to the
very complex. Minimally, a network simulator must enable a user to represent a network
topology, specifying the nodes on the network, the links between those nodes and the traffic
between the nodes.
8. More complicated systems may allow the user to specify everything about the protocols
Exp. No.12
Aim:
To Develop the Simulation for the network with 2 nodes and 1 link.
Theory
First of all, you need to create a simulator object. This is done with the command
set ns [new
Simulator]
Now we open a file for writing that is going to be used for the nam trace data.
set nf [open out.nam w]
$ns namtrace-all $nf
The first line opens the file 'out.nam' for writing and gives it the file handle 'nf'. In the second
line we tell the simulator object that we created above to write all simulation data that is
going to be relevant for nam into this file.The next step is to add a 'finish' procedure that
closes the trace file and starts nam.
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
The next line tells the simulator object to execute the 'finish' procedure after 5.0
seconds of simulation time.
$ns at 5.0 "finish"
# Insert your own code for topology creation
# and agent definitions, etc. here
#Call the finish procedure after 5 seconds simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run
Algorithm
1. Create a simulator object
2. open the nam trace file
3. Define the finish procedure that closes the trace file.
KLNCE/Dept IT 17
Networks Lab
4. Create two nodes no and n1
5. Create a duplex link between the nodes.
6. Create a UDP agent and attach it to node no
7. Create a CBR traffic source and attach it to udp0
8. Create a Null agent which act as a traffic sink and attach it to node n1
9. Connect two agents
10. Set the time for one agent to send data and another agent to receive data.
11. Call the finish procedure after 5 seconds of simulation time
12. Run the Simulation
Result
Thus the simulation for the network with 2 nodes was developed successfully.
Result
Thus the Simulation for the network with 4 nodes was developed successfully.