Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

Computer Network Lab Manual

EX.NO:7 Implementation of Stop and Wait Protocol and


SlidingWindow 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())i

f(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 for implementing stop and wait protocol and

sliding window protocol was executed successfully.


EX.NO:8 Study of Socket Programming and Client – Server model

AIM:

To implement socket programming date and time display from client to server using TCP

Sockets

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:
//TCP Date Server--tcpdateserver.java

import java.net.*;

import java.io.*;

import java.util.*;

class tcpdateserver

public static void main(String arg[])

ServerSocket ss = null;

Socket cs; PrintStream ps;

BufferedReader dis;

String inet; try

ss = new ServerSocket(4444);

System.out.println("Press Ctrl+C to quit"); while(true)

cs = ss.accept();

ps = new PrintStream(cs.getOutputStream());

Date d = new

Date() ps.println(d);

dis = new BufferedReader(new InputStreamReader(cs.getInputStream()));

inet = dis.readLine(); System.out.println("Client System/IP address is :"+

inet); ps.close(); dis.close();


}

catch(IOException e)

{ System.out.println("The exception is :" + e);

// TCP Date Client--tcpdateclient.java

import java.net.*;

import java.io.*;

class tcpdateclient

public static void main (String args[])

{ Socket soc; BufferedReader dis;

String sdate; PrintStream ps;

try { InetAddress ia =

InetAddress.getLocalHost(); if (args.length == 0)

soc = new Socket(InetAddress.getLocalHost(),4444);

else soc = new Socket(InetAddress.getByName(args[0]),4444);

dis = new BufferedReader(new InputStreamReader(soc.getInputStream()));

sdate=dis.readLine(); System.out.println("The date/time on server is : "

+sdate); ps = new PrintStream(soc.getOutputStream());

ps.println(ia);

ps.close();

catch(IOException e)
{

System.out.println("THE EXCEPTION is :" + e);

OUTPUT

$ javac tcpdateserver.java

$ java tcpdateserver

Press Ctrl+C to quit Client System/IP address is : localhost.localdomain/127.0.0.1

Client System/IP address is : localhost.localdomain/127.0.0.1

Client: $

javac tcpdateclient.java $ java tcpdateclient The date/time on server is: Wed Jul 06

07:12:03 GMT 2011

Every time when a client connects to the server, server‟s date/time will be returned to the

client for synchronization.

RESULT:

Thus the program for implementing to display date and time from client to server

using TCP Sockets was executed successfully and output verified using various samples.

EX.NO:9 Write a code simulating ARP /RARP protocols.


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

Program for Reverse Address Resolutuion Protocol (RARP) using UDP

Client:

import java.io.*;

import java.net.*;

import java.util.*;
class Clientrarp12

public static void main(String args[])

try

DatagramSocket client=new DatagramSocket();

InetAddress addr=InetAddress.getByName("127.0.0.1");

byte[] sendbyte=new byte[1024];

byte[] receivebyte=new byte[1024];

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the Physical address (MAC):");

String str=in.readLine();

sendbyte=str.getBytes();

DatagramPacket sender=new

DatagramPacket(sendbyte,sendbyte.length,addr,1309);

client.send(sender);

DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);

client.receive(receiver);

String s=new String(receiver.getData());

System.out.println("The Logical Address is(IP): "+s.trim());

client.close();

}
catch(Exception e)

System.out.println(e);

Server:

import java.io.*;

import java.net.*;

import java.util.*;

class Serverrarp12

public static void main(String args[])

try

DatagramSocket server=new

DatagramSocket(1309); 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. 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();
}

Out put :

Result:

Thus the program was implementing to simulating ping and traceroute commands

EX-NO 11. Write a program to implement RPC (Remote


Procedure Call)
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

EX-NO 12. Implementation of Subnetting


Aim:

Write a program to implement subnetting and find the subnet masks.

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.util.Scanner;

class Subnet
{

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print(―Enter the ip address: ―);

String ip = sc.nextLine(); String

split_ip[] = ip.split(―\\.‖);

//SPlit the string after every .

String split_bip[] = new String[4];

//split binary ip

String bip = ―‖;

for(int i=0;i<4;i++){

split_bip[i] = appendZeros(Integer.toBinaryString(Integer.parseInt(split_ip[i])));

// ―18‖ => 18 => 10010 =>

00010010 bip += split_bip[i];

System.out.println(―IP in binary is ―+bip);

System.out.print(―Enter the number of addresses:

―); int n = sc.nextInt();

//Calculation of mask

int bits = (int)Math.ceil(Math.log(n)/Math.log(2)); /*eg if address = 120, log


120/log 2 gives

log to the base 2 => 6.9068, ceil gives us upper integer */

System.out.println(―Number of bits required for address =

―+bits); int mask = 32-bits;


System.out.println(―The subnet mask is = ―+mask);

//Calculation of first address and last

address int fbip[] = new int[32];

for(int i=0; i<32;i++) fbip[i] = (int)bip.charAt(i)-48; //convert cahracter 0,1 to


integer

0,1 for(int i=31;i>31-bits;i–)//Get first address by ANDing last n bits with 0

fbip[i] &= 0;

String fip[] = {―‖,‖‖,‖‖,‖‖};

for(int i=0;i<32;i++)

fip[i/8] = new String(fip[i/8]+fbip[i]);

System.out.print(―First address is = ―);

for(int i=0;i<4;i++){

System.out.print(Integer.parseInt(fip[i],2));

if(i!=3) System.out.print(―.‖);

System.out.println();

int lbip[] = new int[32];

for(int i=0; i<32;i++) lbip[i] = (int)bip.charAt(i)-48; //convert cahracter 0,1 to


integer 0,1

for(int i=31;i>31-bits;i–)//Get last address by ORing last n bits with 1

lbip[i] |= 1;

String lip[] = {―‖,‖‖,‖‖,‖‖};

for(int i=0;i<32;i++)

lip[i/8] = new String(lip[i/8]+lbip[i]);

System.out.print(―Last address is = ―);

for(int i=0;i<4;i++){

System.out.print(Integer.parseInt(lip[i],2));
if(i!=3) System.out.print(―.‖);

System.out.println();

static String appendZeros(String s){

String temp = new String(―00000000″);

return temp.substring(s.length())+ s;

Output

Enter the ip address: 100.110.150.10

IP in binary is 01100100011011101001011000001010

Enter the number of addresses: 7

Number of bits required for address = 3

The subnet mask is = 29

First address is = 100.110.150.8

Last address is = 100.110.150.15

Result :

Thus the Program was displayed implement subnetting and find the subnet masks.

You might also like