Computer Networks Lab Manual
Computer Networks Lab Manual
LAB MANUAL
Subject Code: KCS-653 H
AIM:
To study about socket programming for developing the client server application.
DESCRIPTION
a= socket(addressfamily,type of socket,protocol)
RESULT:
ALGORITHM
SOCKET BINDING
AIM
To write a C program for socket binding
ALGORITHM
RESULT:
Aim:
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.
Algorithm
Server
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;
}
els
e 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.
EXPERIMENT NO-3
ECHO CLIENT AND SERVER USING TCP SOCKETS
AIM:
ALGORITHM:
4. The client accept the connection and send data to server and the server to replay
5. The client communicate the server to send the end of the message
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;
}
els
e 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.
EXPERIMENT No- 4
File transfer using TCP sockets
Aim
Algorithm
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.
EXPERIMENT NO-5
Write a Code simulating PING and TRACEROUTE Commands
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
EXPERIMENT NO-6
Write a program to implement RPC
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)
EXPERIMENT NO-7
Implementation of Stop and Wait Protocol and Sliding Window Protocol
AIM:
To write a java program to perform sliding window.
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 :
import java.net.*;
import java.io.*;
import java.rmi.*;
public class slidsender
{
public static void main(String a[])throws Exception
{
ServerSocket ser=new ServerSocket(10);
Socket s=ser.accept();
DataInputStream in=new DataInputStream(System.in);
DataInputStream in1=new
DataInputStream(s.getInputStream()); String sbuff[]=new
String[8];
PrintStream p;
int sptr=0,sws=8,nf,ano,i;
String ch;
do
{
p=new PrintStream(s.getOutputStream());
System.out.print("Enter the no. of frames : ");
nf=Integer.parseInt(in.readLine());
p.println(nf);
if(nf<=sws-1)
{
System.out.println("Enter "+nf+" Messages to be send\n");
for(i=1;i<=nf;i++)
{
sbuff[sptr]=in.readLine();
p.println(sbuff[sptr]); sptr=++sptr
%8;
}
sws-=nf;
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.
EXPERIMENT NO-8
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 server‟s date and time to the client.
4. Read client‟s 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 server‟s 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
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.
EXPERIMENT NO-9
Create a socket for HTTP for web page upload and download.
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 {
dos.close();
out.close();
}catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
soc.close();
}
soc.close();
}
}
Server
import java.net.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
class Server {
public static void main(String args[]) throws Exception{
ServerSocket server=null;
Socket socket;
server = new ServerSocket(4000);
System.out.println("Server Waiting for image");
socket = server.accept();
System.out.println("Client connected.");
InputStream in = socket.getInputStream();
DataInputStream dis = new
DataInputStream(in);
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;
int length = 0;
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
EXPERIMENT NO-10
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.
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.
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
used to handle network traffic.
9. Graphical applications allow users to easily visualize the workings of their simulated
environment. Text-based applications may provide a less intuitive interface, but may
permit more advanced forms of customization.
GTNets, are programming-oriented, providing a programming framework that the user then
customizes to create an application that simulates the networking environment to be tested.