59
TABLE OF CONTENTS SL.N O DATE PROGRAM PAG E MARKS SIGN 1 18.1. 11 CHATTING USING UDP 2 18.1. 11 ECHO USING UDP 3 25.1. 11 MULTICASTING USING UDP 4 25.1. 11 MULTICASTING ENCRYPTION AND DECRYPTION 5 1.2.1 1 ECHO USING TCP 6 1.2.1 1 CHATTING USING TCP 7 8.2.1 1 DATE AND TIME SERVER USING TCP 8 15.2. 11 IMPLEMENTATION OF PING COMMAND 9 22.2. 11 REMOTE PROCEDURE CALL 10 1.3.1 1 SLIDING WINDOW PROTOCOL 11 15.3. 11 UNICAST ROUTING PROTOCOL 12 22.3. 11 MULTICAST ROUTING PROTOCOL 13 29.3. CARRIER SENSE MULTIPLE

DocumentCN

Embed Size (px)

Citation preview

Page 1: DocumentCN

TABLE OF CONTENTS

SL.NO DATE PROGRAM PAGE MARKS SIGN

1 18.1.11 CHATTING USING UDP

2 18.1.11 ECHO USING UDP

3 25.1.11 MULTICASTING USING UDP

4 25.1.11 MULTICASTING ENCRYPTION AND DECRYPTION

5 1.2.11 ECHO USING TCP

6 1.2.11 CHATTING USING TCP

7 8.2.11 DATE AND TIME SERVER USING TCP

8 15.2.11 IMPLEMENTATION OF PING COMMAND

9 22.2.11 REMOTE PROCEDURE CALL

10 1.3.11 SLIDING WINDOW PROTOCOL

11 15.3.11 UNICAST ROUTING PROTOCOL

12 22.3.11 MULTICAST ROUTING PROTOCOL

13 29.3.11 CARRIER SENSE MULTIPLE ACCESS

EX.NO:1 CHATTING USING UDP

Page 2: DocumentCN

18.01.11

AIM:

To write a java program for client server application for chatting using UDP.

ALGORITHM:

CLIENT:

Step1: Start the program.

Step2:Include necessary package in java.

Step3:Create a socket in client to server.

Step4:The client establishes a connection to the server.

Step5:Send the data from client to server or viceversa.

Step6:Press ctrl+c to come out of the client.

Step7:Stop the program.

SERVER:

Step1:Start the program.

Step2:Include necessary package in java.

Step3:Create a socket in server to client.

Step4:The server establishes a connection to the client.

Step5:Send the data from server to client or viceversa.

Step6:Press ctrl+c to come out of the server.

Step7:Stop the program.

SOURCE CODE:

Page 3: DocumentCN

CLIENT:

import java.io.*;import java.net.*;class clientchat{public static void main(String args[])throws Exception{BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in));DatagramSocket ClientSocket=new DatagramSocket();InetAddress IPAddress=InetAddress.getByName("localhost");byte[] receiveData=new byte[1024];byte[] sendData=new byte[1024];while(true){String sentence=inFromUser.readLine();sendData=sentence.getBytes();DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IPAddress,9876);ClientSocket.send(sendPacket);DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);ClientSocket.receive(receivePacket);String modifiedsentence=new String(receivePacket.getData());System.out.println("From server:"+modifiedsentence);}}}

SERVER:

Page 4: DocumentCN

import java.io.*;import java.net.*;class serverchat{public static void main(String args[])throws Exception{DatagramSocket ServerSocket=new DatagramSocket(9876);byte[] receiveData=new byte[1024];byte[] sendData=new byte[1024];while(true){DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);ServerSocket.receive(receivePacket);String ssentence=new String(receivePacket.getData());System.out.println("Received Fromclient:"+ssentence);InetAddress IPAddress=receivePacket.getAddress();int port=receivePacket.getPort();BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in));String sentence=inFromUser.readLine();String capitalizedsentence=sentence.toUpperCase();sendData=capitalizedsentence.getBytes();DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IPAddress,port);ServerSocket.send(sendPacket);}}}

RESULT:

Thus the Java program to implement chatting using UDP was executed successfully and the output was verified.

EX.NO:2 ECHO USING UDP

Page 5: DocumentCN

18.01.11

AIM:

To write a java program for client server application for sending message to server from client with echo using UDP.

ALGORITHM:

CLIENT:

Step 1 :Start the program.

Step 2 :Include the necessary packages in Java.

Step 3 :To create aa socket in client to server.

Step 4 :The client establishes a connection to the server.

Step 5 :Send the data from client to server.

Step 6 : Get the echo from the server and display the string.

Step 7 :Stop the program.

SERVER:

Step 1 :Start the program.

Step 2 :Include the necessary packages in Java.

Step 3 :To create a socket in server to client.

Step 4 :The server establishes a connection to the client using a port number.

Step 5 :Get the message from the client port number.

Step 6 :Capitalize the received message.

Step 7 :Send that message to the client.

Step 8 :Stop the program.

Page 6: DocumentCN

SOURCE CODE:

CLIENT:

import java.io.*;import java.net.*;class clientecho{public static void main(String args[])throws Exception{BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in));DatagramSocket clientSocket=new DatagramSocket();InetAddress IpAddress=InetAddress.getByName("localhost");byte[] receiveData=new byte[1024];byte[] sendData=new byte[1024];String sentence=inFromUser.readLine();sendData=sentence.getBytes();DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IpAddress,9876);clientSocket.send(sendPacket);DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);clientSocket.receive(receivePacket);String modifiedsentence=new String(receivePacket.getData());System.out.println("from server"+modifiedsentence);clientSocket.close();}}

SERVER:

Page 7: DocumentCN

import java.io.*;import java.net.*;class serverecho{public static void main(String args[])throws Exception{DatagramSocket serverSocket=new DatagramSocket(9876);byte[] receiveData=new byte[1024];byte[] sendData=new byte[1024];while(true){DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);serverSocket.receive(receivePacket);String sentence=new String(receivePacket.getData());System.out.println("received"+sentence);InetAddress IpAddress=receivePacket.getAddress();int port=receivePacket.getPort();String capitalizedsentence=sentence.toUpperCase();sendData=capitalizedsentence.getBytes();DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IpAddress,port);serverSocket.send(sendPacket);}}}

RESULT:

Thus the Java program to implement echo using UDP was executed successfully and the output was verified.

EX.NO:3 MULTICASTING USING UDP

Page 8: DocumentCN

25.01.11

AIM:

To write a java program to implement multicasting in Java.

ALGORITHM:

CLIENT:

Step 1 :Start the program.

Step 2 :Include necessary header files.

Step 3 :Get the port number of server.

Step 4 :Assign the IP address.

Step 5 :Stop the program.

SERVER:

Step 1 :Start the program.

Step 2 :Include necessary header files.

Step 3 :Send the message to client.

Step 4 :End the editor.

Step 5 :Stop the program.

SOURCE CODE:

Page 9: DocumentCN

CLIENT:

import java.io.*;import java.net.*;class mc{public static void main(String args[])throws Exception{DatagramPacket dp;MulticastSocket ds;String s;byte b[]=new byte[2048];ds=new MulticastSocket(1234);InetAddress add=InetAddress.getByName("230.0.0.1");ds.joinGroup(add);while(true){dp=new DatagramPacket(b,2048);ds.receive(dp);s=new String(dp.getData());System.out.println("received(client)"+s.trim());}}}

Page 10: DocumentCN

SERVER:

import java.io.*;import java.net.*;class ms{public static void main(String args[])throws Exception{String sendmsg=new String(); String a="end";DatagramSocket ds;int len=0;byte b[]=new byte[2048];ds=new DatagramSocket(1117);BufferedReader br=new BufferedReader(new InputStreamReader(System.in));while(!sendmsg.equals(a)){System.out.println("enter the text server");sendmsg=br.readLine();len=sendmsg.length();ds.send(new DatagramPacket(sendmsg.getBytes(),len,InetAddress.getByName("230.0.0.1"),1234));}}}

RESULT:

Thus the Java program to implement multicasting using UDP was executed successfully and the output was verified.

EX.NO:4 MULTICAST ENCRYPTION AND DECRYPTION

25.01.11

Page 11: DocumentCN

AIM:

To write a Java program to implement encryption and decryption using multicast.

ALGORITHM:

PACKAGE ENCRYPTION:

Step 1 :Start the program.

Step 2 :Include the necessary packages in Java.

Step 3 :Define a class decr and declare the necessary variables.

Step 4 :Perform the function decrypte.

Step 5 :Get the public key,prime value and primary key.

Step 6 :Perform the while loop.

Step 7 :Define a class encr and declare the necessary variables.

Step 8 :Perform encrypt function.

Step 9 :Get the number of users,public key,data,prime value and primary key.

Step10:Perform the while loop.

Step11:Stop the program.

CLIENT:

Step 1 :Start the program.

Step 2 :Import the package.

Step 3 :Get the port number of server and assign group IP address.

Step 4 :Receive the packet and perform trim function.

Page 12: DocumentCN

Step 5 :Decrypt the data.

Step 6 :Stop the program.

SERVER:

Step 1 :Start the program.

Step 2 :Import the package.

Step 3 :Assign the group IP address.

Step 4 :Call the method encrypte() in the package.

Step 5 :Get the encrypted data.

Step 6 :Stop the program.

SOURCE CODE:

PACKAGE ENCRYPTION:

package encryption;import java.io.*;import java.util.Scanner;import java.lang.*;public class encr{double data,prime,pub_key,result=1;double[] pri_key=new double[10];double[] p=new double[10];double cipher=0,plain;int n;

public double encrypte(){Scanner ip=new Scanner(System.in);

Page 13: DocumentCN

System.out.println("Enter the no of users");n=ip.nextInt();System.out.println("Enter the public key");pub_key=ip.nextDouble();System.out.println("Enter the data");data=ip.nextDouble();System.out.println("Enter the prime value");prime=ip.nextDouble();System.out.println("Enter the primary key");

for(int i=0;i<n;i++){pri_key[i]=ip.nextDouble();}

for(int i=0;i<n;i++){p[i]=((Math.pow(pub_key,pri_key[i]))%prime);}

for(int i=0;i<n;i++){double temp=p[i],f=1;while(temp>=1){temp=temp/10;f=f*10;}p[i]=p[i]+f;result=result*p[i];}

for(int i=0;i<n;i++){cipher=data+result;}return cipher;

Page 14: DocumentCN

}}

PACKAGE ENCRYPTION:package encryption;import java.io.*;import java.util.Scanner;import java.lang.*;public class decr{double prime,pub_key,result=1;double pri_key,p;double plain;int n;public double decrypte(double ciph){Scanner ip=new Scanner(System.in);System.out.println("Enter the public key");pub_key=ip.nextDouble();System.out.println("Enter the prime value");prime=ip.nextDouble();System.out.println("Enter the primary key");pri_key=ip.nextDouble();p=((Math.pow(pub_key,pri_key))%prime);double temp=p,f=1;while(temp>=1){temp=temp/10;f=f*10;}p=p+f;plain=ciph%p;return plain;}}CLIENT:

import java.net.*;import java.util.*;

Page 15: DocumentCN

import encryption.*;public class mclient{public static void main(String args[])throws Exception{double rec=0,rec1;char c;decr d=new decr();MulticastSocket socket;DatagramPacket packet;InetAddress add;add=InetAddress.getByName("233.1.1.1");socket=new MulticastSocket(1200);socket.joinGroup(add);byte[] data=new byte[2048];packet=new DatagramPacket(data,data.length);while(true){socket.receive(packet);String str=new String(packet.getData());str=str.trim();for(int i=0;i<str.trim().length()-2;i++){c=str.charAt(i);rec=rec*10+(c-48);}rec1=d.decrypte(rec);System.out.println("Received client(encrypted value):"+rec);System.out.println("Received client(decrypted value):"+rec1);}}}

SERVER:

import java.net.*;import java.util.Scanner;

Page 16: DocumentCN

import encryption.*;public class mserver{public static void main(String args[])throws Exception{encr e=new encr();double input;String st;MulticastSocket socket;DatagramPacket packet;InetAddress add;add=InetAddress.getByName("233.1.1.1");socket=new MulticastSocket();socket.joinGroup(add);byte[] data=new byte[2048];while(true){Scanner br=new Scanner(System.in);input=e.encrypte();System.out.println("encrypted data:"+input);st=""+input;data=st.getBytes();packet=new DatagramPacket(data,st.length(),add,1200);socket.send(packet);String str=new String(packet.getData());}}}

RESULT:

Thus the Java program to implement multicast encryption and decryption was executed successfully and the output was verified.

EX.NO:5 ECHO USING TCP

01.02.11

Page 17: DocumentCN

AIM:

To write a java program for echo server and client for passing information from client to server using TCP.

ALGORITHM:

CLIENT:

Step 1 :Start the program.

Step 2 :Include necessary packages in Java.

Step 3 :To create a socket in client to server.

Step 4 :The client establishes a connection to server.

Step 5 :The client accepts the connection to send data from client to server.

Step 6 :The client sends the data and it sends the acknowledge to server.

Step 7 :Stop the program.

SERVER:

Step 1 :Start the program.

Step 2 :Include the necessary packages in Java.

Step 3 :To create server socket in server to client.

Step 4 :The server establishes a connection to the client.

Step 5 :The server accepts the connection to send echo to server.

Step 6 :The server sends the echo and sends the acknowledgement to the client.

Step 7 :Stop the program.

SOURCE CODE:

CLIENT:

Page 18: DocumentCN

import java.io.*;

import java.net.*;

public class tcpcli

{

public static void main(String args[])

{

Socket c=null;

String line;

DataInputStream is,is1;

PrintStream os;

try

{

c=new Socket("127.0.0.1",9999);

}

catch(IOException e)

{

}

try

{

os=new PrintStream(c.getOutputStream());

is=new DataInputStream(System.in);

is1=new DataInputStream(c.getInputStream());

Page 19: DocumentCN

System.out.println("Type quit to exit");

do

{

System.out.println("client:");

line=is.readLine();

os.println(line);

System.out.println("FROM SERVER:"+is1.readLine());

}

while(line.equalsIgnoreCase("quit")==false);

is1.close();

os.close();

}

catch(IOException e)

{

System.out.println("SOCKET CLOSED");

}

}

}

SERVER:

import java.io.*;

Page 20: DocumentCN

import java.net.*;

import java.util.*;

public class tcpser

{

public static void main(String args[])

{

Socket c=null;

String line;

DataInputStream is=null,is1=null;

PrintStream os=null;

ServerSocket s=null;

try

{

s=new ServerSocket(9999);

}

catch(IOException e)

{

System.out.println(e);

}

try

{

c=s.accept();

Page 21: DocumentCN

is=new DataInputStream(c.getInputStream());

is1=new DataInputStream(System.in);

os=new PrintStream(c.getOutputStream());

do

{

line=is.readLine();

System.out.println("from client:"+line);

String line1=line.toUpperCase();

os.println(line1);

}

while(line.equalsIgnoreCase("quit")==false);

is.close();

os.close();

}

catch(IOException e){ }

}

}

RESULT:

Thus the Java program to implement echo using TCP was executed successfully and the output was verified.

EX.NO:6 CHATTING USING TCP

01.02.11

Page 22: DocumentCN

AIM: To write a client-server application for chatting using TCP.

ALGORITHM:

CLIENT:

Step1:Start the program.

Step2:Include the necessary package in java.

Step3:To create a socket in client to server.

Step4:The client established a connection to the server.

Step5:The client accept the connection and to send the data from client to server and viceversa.

Step6:The client communicate the server to send the end of the message.

Step7:Stop the program.

SERVER:

Step1:Start the program.

Step2: :Include the necessary package in java.

Step3:To create a socket in server to client.

Step4:The server established a connection to the client .

Step5:The server accept the connection and to send the data from server

To client and viceversa.

Step6:The client communicate the server to send the end of the message.

Step7:Stop the program.

SOURCE CODE:

CLIENT:import java.io.*;

Page 23: DocumentCN

import java.net.*;public class tc{public static void main(String args[]){Socket c=null;String line;try{c=new Socket("127.0.0.1",9999);}catch(IOException e){}try{PrintStream os=new PrintStream(c.getOutputStream());DataInputStream is=new DataInputStream(System.in);DataInputStream is1=new DataInputStream(c.getInputStream());do{System.out.println("client:");line=is.readLine();os.println(line);System.out.println("SERVER:"+is1.readLine());}while(line.equalsIgnoreCase("quit")==false);is1.close();os.close();}catch(IOException e){ }}}

SERVER:import java.io.*;import java.net.*;

Page 24: DocumentCN

import java.util.*;public class ts{public static void main(String args[])throws Exception{Socket c=null; String line;DataInputStream is=null,is1=null;PrintStream os=null;ServerSocket s=null;try{s=new ServerSocket(9999);}catch(IOException e) { }c=s.accept();is=new DataInputStream(c.getInputStream());is1=new DataInputStream(System.in);os=new PrintStream(c.getOutputStream());do{line=is.readLine();System.out.println("from client:"+line);System.out.println("server:");line=is1.readLine();os.println(line);}while(line.equalsIgnoreCase("quit")==false);is.close();os.close();}}}

RESULT:

Thus the Java program to implement chatting using TCP was executed successfully and the output was verified.

EX.NO:7 DATE AND TIME SERVER USING TCP

Page 25: DocumentCN

08.02.11

AIM:

To write a client-server application for chatting using TCP and also display the date.

ALGORITHM:

CLIENT:

Step1:Start the program.

Step2:Include the necessary package in java.

Step3:To create a socket in client to server.

Step4:Then communicate with server to send message to get date and time.

Step5:Stop the program.

SERVER:

Step1:Start the program.

Step2:Include the necessary package in java.

Step3:Include the utility package with Necessary classes.

Step4:To create a socket in server to client.

Step5:If the client asks the date and time,then using the class date find the current date and time.

Step6:Then send it to client.

Step7:Stop the program.

SOURCE CODE:

Page 26: DocumentCN

CLIENT:import java.io.*;import java.net.*;import java.util.*;import java.text.*;public class tcpclidate{public static void main(String args[])throws Exception{Socket c=null;String line;DataInputStream is,is1;PrintStream os;try{c=new Socket("127.0.0.1",9999);}catch(IOException e){System.out.println(e);}try{os=new PrintStream(c.getOutputStream());is=new DataInputStream(System.in);is1=new DataInputStream(c.getInputStream());do{System.out.println("client");line=is.readLine();os.println(line);System.out.println("server:"+is1.readLine());}while(line.equalsIgnoreCase("quit")==false);is1.close();os.close();}catch(IOException e){System.out.println("socket closed,message passing is over");

Page 27: DocumentCN

}}}

SERVER:

import java.io.*;import java.net.*;import java.util.*;import java.text.*;public class tcpserverdate{public static void main(String args[])throws Exception{Socket c=null;ServerSocket s=null;String line;DataInputStream is,is1;PrintStream os=null;Date date=new Date();SimpleDateFormat d;try{s=new ServerSocket(9999);}catch(IOException e){System.out.println(e);}try{c=s.accept();is=new DataInputStream(c.getInputStream());is1=new DataInputStream(System.in);os=new PrintStream(c.getOutputStream());do{

Page 28: DocumentCN

line=is.readLine();System.out.println("client"+line);System.out.println("server");if(line.equalsIgnoreCase("date")){d=new SimpleDateFormat("dd/mm/yyyy hh:mm:ss zzz");String str=d.format(date);os.println(str);System.out.println(str);}else{line=is1.readLine();os.println(line);}}while(line.equalsIgnoreCase("quit")==false);is.close();os.close();}catch(IOException e){System.out.println("socket closed");}}}

RESULT:

Thus the Java program to implement date and time server using TCP was executed successfully and the output was verified.

EX.NO:8 IMPLEMENTATION OF PING COMMAND

Page 29: DocumentCN

15.02.11

AIM:

TO write a java program to implement the ping command.

ALGORITHM:

Step1:Start the program.

Step2:Include the necessary package in java.

Step3:Create a process object p to implement the ping command.

Step4:Declare one BufferedReader stream class object.

Step5:Get the details of server ie.length of ip address,time required to get the details,send packets,received packets and lost packets and minimum,maximum and average times.

Step6:Print the results.

Step7:Stop the program.

SOURCE CODE:

Page 30: DocumentCN

import java.io.*;import java.net.*;import java.util.*;public class ping{public static void main(String[] args)throws IOException{String ip="127.0.0.1";String pingResult="";String pingCmd="ping "+ip;try{Runtime r=Runtime.getRuntime();Process p=r.exec(pingCmd);BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));String inputLine;while((inputLine=in.readLine())!=null){System.out.println(inputLine);pingResult+=inputLine;}in.close();}catch(IOException e){System.out.println(e);}}}

RESULT:

Thus the Java program to implement PING command was executed successfully and the output was verified.

EX.NO:9 REMOTE PROCEDURE CALL

Page 31: DocumentCN

22.02.11

AIM:

To write a Java program to implement the RPC.

ALGORITHM:

Step 1 :Start the program.

Step2 :Declare the necessary packages in Java.

Step 3 :Create an interface as UCET and extend the predefined interface remote

Into it.

Step 4 :Declare the remote methods inside the interface UCET.

Step 5 :Declare the class rpc and extend the predefined class

UniCastRemoteObject and implement the interface UCET into it.

Step 6 :Write the remote methods definition into the class rpc.Step 7 :Declare the class server and create an object ob for the class rpc.

Step 8 :Declare the class client and call the remote methods using the object ob

Which is the object of interface UCET.

Step 9 :The remote method contains the methods such as function(a,b),

power(a,b) and log(a).

Step 10 :Stop the program.

SOURCE CODE:

Page 32: DocumentCN

CLIENT:

import java.io.*;import java.rmi.*;import java.rmi.server.*;public class client{public static void main(String args[]){try{DataInputStream s=new DataInputStream(System.in);ucet ob=(ucet)Naming.lookup("IT");System.out.println("Enter the values of a and b");int a=Integer.parseInt(s.readLine());int b=Integer.parseInt(s.readLine());int r=ob.function(a,b);System.out.println("The answer of (a+b)^2 is:"+r);int t=ob.power(a,b);System.out.println("The answer of(a)^(b) is:"+t);double d=ob.log(a);System.out.println("The log value of the given number "+a+" is:"+d);}catch(Exception e){System.out.println(e);}}}

SERVER:

Page 33: DocumentCN

import java.rmi.*;import java.rmi.server.*;public class server{public static void main(String s[]){try{rpc ob=new rpc();Naming.rebind("IT",ob);}catch(Exception e){}}}

RPC:

import java.rmi.*;import java.lang.Math;import java.rmi.server.*;public class rpc extends UnicastRemoteObject implements ucet{public rpc() throws RemoteException{}public int function(int a,int b) throws RemoteException{int m;m=(a*a)+(b*b)+(2*a*b);return m;}public int power(int a,int b) throws RemoteException{int m=(int)Math.pow(a,b);

Page 34: DocumentCN

return m;}public double log(int a) throws RemoteException{return(Math.log(a));}}

UCET:

import java.rmi.*;public interface ucet extends Remote{public int function(int a,int b) throws RemoteException;public int power(int a,int b) throws RemoteException;public double log(int a) throws RemoteException;}

RESULT:

Thus the Java program to implement Remote Procedure Call (RPC) was executed successfully and the output was verified.

EX.NO:10 SLIDING WINDOW PROTOCOL

Page 35: DocumentCN

01.03.11

AIM:

To write a java program to implement sliding window protocol.

ALGORITHM:

CLIENT:

Step 1 :Start the program.

Step 2 :Include the necessary packages.

Step 3 :Create a socket in client to server.

Step 4 :The client establishes a connection to the server.

Step 5 :Get the number of frames that are going to receive.

Step 6 :Get the nf number of messages from server and display it.

Step 7 :Send the acknowledgement for the last frame received plus one.

Step 8:Stop the program.

SERVER:

Step 1 :Start the program.

Step 2 :Include the necessary packages.

Step 3 :Accept the socket from the client.

Step 4 :The server establishes a connection to the client.

Step 5 :Get the frames and messages less than the given window size.

Step 6 :Send the messages to the client.

Step 7 :Stop the program.

SOURCE CODE:

Page 36: DocumentCN

CLIENT:

import java.io.*;

import java.net.*;

class slideclient

{

public static void main(String args[])throws Exception

{

Socket s=new Socket(InetAddress.getLocalHost(),10);

DataInputStream dis=new DataInputStream(s.getInputStream());

PrintStream p=new PrintStream(s.getOutputStream());

int i=0,rptr=-1,nf,rws=10;

String rtrf[]=new String[10];

String ch;

do

{

nf=Integer.parseInt(dis.readLine());

if(nf<=rws-1)

{

for(i=1;i<=nf;i++)

{

rptr=++rptr%10;

rtrf[rptr]=dis.readLine();

Page 37: DocumentCN

System.out.println("Frame"+rptr+" "+rtrf[rptr]);

}

rws-=nf;

System.out.println("Acknowledgement sent.....\n");

p.println(rptr+1);

rws+=nf;

}

else

break;

ch=dis.readLine();

}

while(ch.equals("yes"));

}}

SERVER:

import java.io.*;

import java.net.*;

class slideserver

{

public static void main(String args[])throws Exception

{

ServerSocket ser=new ServerSocket(10);

Page 38: DocumentCN

Socket s=ser.accept();

DataInputStream d=new DataInputStream(System.in);

DataInputStream d1=new DataInputStream(s.getInputStream());

String strf[]=new String[10];

PrintStream p;

int sptr=0,sws=10,nf,ano,i;

String ch;

do

{

p=new PrintStream(s.getOutputStream());

System.out.println("Enter the number of frames:");

nf=Integer.parseInt(d.readLine());

p.println(nf);

if(nf<=sws-1){

System.out.println("Enter"+nf+"message\n");

for(i=1;i<=nf;i++){

strf[sptr]=d.readLine();

p.println(strf[sptr]);

sptr=++sptr%10;}

sws-=nf;

System.out.println("Acknowledgement received\n");

ano=Integer.parseInt(d1.readLine());

Page 39: DocumentCN

System.out.println("Ack"+ano);

sws+=nf;

}

else

{

System.out.println("Exceeds windows size");

break;

}

System.out.println("Do you want to send some more frame:");

ch=d.readLine();

p.println(ch);

}

while(ch.equals("yes"));

s.close();

} }

RESULT:

Thus the program is created for sliding window protocol and the output is verified by sending the frames from server to client and acknowledgement is received from client.

Page 40: DocumentCN

EX.NO:11 UNICAST ROUTING PROTOCOL

15.03.11

AIM:

To write a ns2 program for implementing unicast routing protocol.

ALGORITHM:

Step 1: start the program.

Step 2: Declare the global variable ns for creating a new simulator.

Step 3: Set the color for packets.

Step 4: Open the network animator file in the name of file2 in the write mode.

Step 5: Open the trace file in the name of file1 in the write mode.

Step 6: Set the unicast routing protocol to transfer the packets in network.

Step 7: Create the required number of nodes.

Step 8: Create the duplex-link between the nodes including the delay time, bandwidth and dropping queue mechanism.

Step 9: Give the position for the links between the nodes.

Step 10: Set a tcp reno connection for source node.

Step 11: Set the destination node using tcp sink.

Step 12: Setup a ftp connection over the tcp connection.

Step 13: Down the connection between any nodes at a particular time.

Step 14: Reconnect the downed connection at a particular time.

Step 15: Define the finish procedure.

Page 41: DocumentCN

Step 16: In the destination of the finish procedure declare the global variables ns, file1, file2.

Step 17: Close the trace file and nam file and execute the network animator file.

Step 18: At the particular time call the finish procedure.

Step 19: Stop the program.

SOURCE CODE:

set ns [new Simulator]$ns color 1 blue$ns color 2 purpleset file1 [open out.tr w]$ns trace-all $file1set file2 [open out.nam w]$ns namtrace-all $file2proc finish {} {global ns file1 file2$ns flush-traceclose $file1close $file2exec nam out.nam &exit 0}$ns rtproto Sessionset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set n4 [$ns node]set n5 [$ns node]set n6 [$ns node]set n7 [$ns node]$ns duplex-link $n0 $n1 0.3Mb 10ms RED$ns duplex-link $n1 $n2 0.3Mb 10ms RED

Page 42: DocumentCN

$ns duplex-link $n2 $n6 0.3Mb 10ms RED$ns duplex-link $n1 $n3 0.3Mb 10ms RED$ns duplex-link $n3 $n4 0.3Mb 10ms RED$ns duplex-link $n4 $n5 0.3Mb 10ms RED$ns duplex-link $n5 $n6 0.3Mb 10ms RED$ns duplex-link $n6 $n7 0.3Mb 10ms RED$ns duplex-link-op $n0 $n1 orient right$ns duplex-link-op $n1 $n2 orient right$ns duplex-link-op $n2 $n6 orient right$ns duplex-link-op $n1 $n3 orient up$ns duplex-link-op $n3 $n4 orient up-left$ns duplex-link-op $n4 $n5 orient left-up$ns duplex-link-op $n5 $n6 orient right-up$ns duplex-link-op $n6 $n7 orient up-right

#setup a TCP connection tcp [new Agent/TCP/Newreno]set tcp [new Agent/TCP/Newreno]$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink/DelAck]$ns attach-agent $n7 $sink$ns connect $tcp $sink$tcp set fid_ 2

#setup a FTP over TCP connectionset ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP$ns rtmodel-at 1.0 down $n1 $n2$ns rtmodel-at 4.5 up $n1 $n2$ns at 0.1 "$ftp start"$ns at 12.0 "finish"$ns runRESULT:

Thus the program is executed and the output is displayed in the network animator file.

Page 43: DocumentCN

EX.NO:12 MULTICAST ROUTING PROTOCOL

22.03.11

AIM:

To write a ns2 program for implementing multicast routing protocol.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the global variables ns for creating a new simulator.

Step 3: Set the color for packets.

Step 4: Open the network animator file in the write mode.

Step 5: Open the trace file in the write mode.

Step 6: Set the multicast routing protocol to transfer the packets in the network.

Step 7: Allocate a multicast address for the group.

Step 8: Create the multicast capable nodes.

Step 9: Create the duplex-link between the nodes.

Step 10: Set a udp connection for source node.

Step 11: Set the destination node using udp null.

Step 12: Create the traffic generator CBR for the source and the destination files.

Step 13: Set the destination address, port and random-false for the source and the destination files.

Step 14: Create the receiver agent for joining and leaving of the nodes in the group.

Step 15: Define the finish procedure.

Step 16: In the definition of the finish procedure declare the global variables ns.

Page 44: DocumentCN

Step 17: Close the trace file and nam file and execute the network animator file.

Step 18: At the particular tme call the finish procedure.

Step 19: Stop the program.

SOURCE CODE:

set ns [new Simulator -multicast on]set f [open out.tr w]$ns trace-all $f$ns namtrace-all [open out.nam w]$ns color 1 Red$ns color 30 Purple$ns color 31 Greenset group [Node allocaddr]set nod 7for {set i 1} {$i <=$nod} {incr i} {set n$i [$ns node]}$ns duplex-link $n1 $n2 0.3Mb 10ms DRR$ns duplex-link $n2 $n3 0.3Mb 10ms DRR$ns duplex-link $n2 $n5 0.3Mb 10ms DRR$ns duplex-link $n2 $n4 0.3Mb 10ms DRR$ns duplex-link $n3 $n4 0.3Mb 10ms DRR$ns duplex-link $n4 $n5 0.3Mb 10ms DRR$ns duplex-link $n4 $n7 0.3Mb 10ms DRR$ns duplex-link $n5 $n6 0.3Mb 10ms DRR$ns duplex-link $n6 $n7 0.3Mb 10ms DRRDM set RP_($group) $n2$ns mrtproto DMset udp1 [new Agent/UDP]set udp2 [new Agent/UDP]$ns attach-agent $n1 $udp1$ns attach-agent $n2 $udp2set src1 [new Application/Traffic/CBR]$src1 attach-agent $udp1

Page 45: DocumentCN

$udp1 set dst_addr_ $group$udp1 set dst_port_ 0$udp1 set fid_ 31$src1 set random_ falseset src2 [new Application/Traffic/CBR]$src2 attach-agent $udp2$udp2 set dst_addr_ $group$udp2 set dst_port_ 1$udp2 set fid_ 30$src2 set random_ falseset rcvr [new Agent/LossMonitor]$ns at 0.6 "$n3 join-group $rcvr $group"$ns at 1.3 "$n4 join-group $rcvr $group"$ns at 1.6 "$n5 join-group $rcvr $group"$ns at 1.9 "$n4 leave-group $rcvr $group"$ns at 2.3 "$n6 join-group $rcvr $group"$ns at 3.2 "$n7 join-group $rcvr $group"$ns at 0.4 "$src1 start"$ns at 2.0 "$src2 start"$ns at 4.0 "finish"proc finish {} {global ns$ns flush-traceexec nam out.nam &exit 0}$ns run

RESULT:

Thus the program is executed and the output is displayed in the network animator file.

Page 46: DocumentCN

EX.NO:13 CARRIER SENSE MULTIPLE ACCESS

29.03.11

AIM:

To write a ns2 program for implementing carrier sense multiple access.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the global variables ns for creating a new simulator.

Step 3: Set the color for packets.

Step 4: Open the network animator file in the write mode.

Step 5: Open the trace file and the win file in the write mode.

Step 6: Create the capable nodes.

Step 7: Ceate the duplex-link between the nodes.

Step 8: Set the lan network to the specific nodes including the bandwidth, delay & queue type.

Step 9: Set the positions for the nodes.

Step 10: Set a tcp reno connection for source node.

Step 11: Set the destination node using tcp sink.

Step 12: Set the window size and the packet size for the tcp.

Step 13: Set up the ftp over tcp connection.

Step 14: Set the udp connection for the source and destination .

Step 15: Create the traffic generator CBR for the source and the destination files.

Step 16: Start and stop the connections of the ftp and cbr at a particular time.

Page 47: DocumentCN

Step 17: Define the plot window and finish procedure.

Step 18: Define the global variables inside the procedure finish.

Step 19: Close the trace file and nam file and execute the network animator file.

Step 20: At the particular time call the finish procedure.

Step 21: Stop the program.

SOURCE CODE:

set ns [new Simulator]$ns color 1 green$ns color 2 purpleset fi1 [open out.tr w]set winfile [open WinFile w]$ns trace-all $fi1set fi2 [open out.nam w]$ns namtrace-all $fi2

proc finish {} {global ns fi1 fi2$ns flush-traceclose $fi1close $fi2exec nam out.nam &exit 0}

set n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set n4 [$ns node]set n5 [$ns node]set n6 [$ns node]

Page 48: DocumentCN

$n1 color red$n1 shape boxs$ns duplex-link $n0 $n2 2Mb 10ms DropTail$ns duplex-link $n1 $n2 2Mb 10ms DropTail$ns duplex-link $n3 $n6 2Mb 10ms DropTail$ns simplex-link $n2 $n3 0.5Mb 100ms DropTail$ns simplex-link $n3 $n2 0.5Mb 100ms DropTailset lan [$ns newLan "$n3 $n4 $n5 $n6" 0.5Mb 40ms LL Queue/DropTail MAC/Csma/Cd Channel]set tcp [new Agent/TCP/Newreno]$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink/DelAck]$ns attach-agent $n4 $sink$ns connect $tcp $sink$tcp set fid_ 1$tcp set window_ 5000$tcp set packetsize_ 552set ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTPset udp [new Agent/UDP]$ns attach-agent $n1 $udpset null [new Agent/Null]$ns attach-agent $n6 $null$ns connect $udp $null$udp set fid_ 2set cbr [new Application/Traffic/CBR]$cbr attach-agent $udp$cbr set type_ CBR$cbr set packet_size_ 1000$cbr set rate_ 0.02mb$cbr set random_ false$ns at 0.3 "$cbr start"$ns at 2.2 "$ftp start"$ns at 15.0 "$ftp stop"$ns at 15.6 "$cbr stop"

Page 49: DocumentCN

proc plotwindow { tcpSource file } {global nsset time 0.1set now [$ns now]set cwnd [$tcpSource set cwnd_]set wnd [$tcpSource set window_]puts $file "$now $cwnd"$ns at [expr $now+$time] "plotwindow $tcpSource $file"}$ns at 0.1 "plotwindow $tcp $winfile"$ns at 5 "$ns trace-annotate \"packet drop\""$ns at 100.0 "finish"$ns run

RESULT:

Thus the program is executed and the output is displayed in the network animator file.