Tuesday, 4 November 2014

SIMULATE STOP AND WAIT ARQ

SIMULATE STOP AND WAIT ARQ

import java.applet.Applet;
import java.applet.*;
import java.awt.*;
public class STOP_WAIT extends Applet {

    public void init()
    {

    }
public void paint(Graphics g)
    {
    g.drawString("Sender",25,40);
    g.drawString("Receiver",325,40);
    g.drawRect(20,20,60,30);//sender Rectangle
    g.drawLine(50,50,50,200);//sender Vertical line
    g.drawRect(320,20,60,30);//Receiver Rectangle
    g.drawLine(350,50,350,200);//receiver Vertical line

    /*Line for Frame 0*/
    g.drawLine(50,60,350,60 );
    g.drawLine(350,60,50,100);
   /*Line for Frame 1*/
    g.drawLine(50,100,350,100 );
    g.drawLine(350,100,50,140);
    /*Line for Frame 2*/
    g.drawLine(50,140,350,140 );
    g.drawLine(350,140,50,180);
   /*Line for Frame 3*/
    g.drawLine(50,180,350,180 );
   
    g.drawString("Frame 0",190,60);
    g.drawString("Frame 1",190,100);
    g.drawString("Frame 2",190,140);
    g.drawString("Frame 3",190,180);
   
    g.drawString("ACK 0",100,90);
    g.drawString("ACK 1",100,130);
    g.drawString("ACK 2",100,170);
   
    g.drawString("S=0",25,65);
    g.drawString("S=1",25,105);
    g.drawString("S=0",25,145);
    g.drawString("S=1",25,185);
   
    g.drawString("R=0",360,65);
    g.drawString("R=1",360,105);
    g.drawString("R=0",360,145);
    g.drawString("R=1",360,185);
    }

}

--------------------------------------------------OUTPUT----------------------------------------------------
        



TO IMPLEMENT SELECTIVE REPEAT ARQ PROTOCOL



TO IMPLEMENT SELECTIVE REPEAT ARQ PROTOCOL

import java.applet.*;
import java.awt.*;
public class SELECTIVE_REPEAT extends Applet {

    public void init()
    {

    }
public void paint(Graphics g)
    {
    g.drawString("Sender",50,40);
    g.drawString("Receiver",325,40);
    g.drawRect(40,20,60,30);//seder
    g.drawLine(70,50,70,200);//sender line
    g.drawRect(320,20,60,30);//Receiver
    g.drawLine(350,50,350,200);//receiver line

    /*Line for Frame 0*/
    g.drawLine(70,60,350,60 );
    g.drawLine(350,60,70,100);
/*Line for Frame 1*/
    g.drawLine(70,100,350,100 );
    g.drawLine(350,100,70,140);
/*Line for Frame 2*/
    g.drawLine(70,140,350,140 );
    g.drawLine(350,140,70,180);
/*Line for Frame 3*/
    g.drawLine(70,180,350,180 );
   
    g.drawString("Frame 0",190,60);
    g.drawString("Frame 1",190,100);
    g.drawString("Frame 2",190,140);
    g.drawString("Frame 3",190,180);

    g.drawString("ACK 0",100,90);
    g.drawString("ACK 1",100,130);
    g.drawString("ACK 2",100,170);
// sender frames
    g.drawRect(5,55,15,15);
    g.drawRect(20,55,15,15);
    g.drawRect(35,55,15,15);
    g.drawRect(50,55,15,15);

    g.drawRect(5,95,15,15);
    g.drawRect(20,95,15,15);
    g.drawRect(35,95,15,15);
    g.drawRect(50,95,15,15);

    g.drawRect(5,135,15,15);
    g.drawRect(20,135,15,15);
    g.drawRect(35,135,15,15);
    g.drawRect(50,135,15,15);

    g.drawRect(5,175,15,15);
    g.drawRect(20,175,15,15);
    g.drawRect(35,175,15,15);
    g.drawRect(50,175,15,15);
//Receiver Frames
    g.drawRect(355,55,15,15);
    g.drawRect(370,55,15,15);
    g.drawRect(385,55,15,15);
    g.drawRect(400,55,15,15);

    g.drawRect(355,95,15,15);
    g.drawRect(370,95,15,15);
    g.drawRect(385,95,15,15);
    g.drawRect(400,95,15,15);

    g.drawRect(355,135,15,15);
    g.drawRect(370,135,15,15);
    g.drawRect(385,135,15,15);
    g.drawRect(400,135,15,15);

    g.drawRect(355,175,15,15);
    g.drawRect(370,175,15,15);
    g.drawRect(385,175,15,15);
    g.drawRect(400,175,15,15);

    g.drawString("0",9,67);
    g.drawString("1",24,67);
    g.drawString("2",39,67);
    g.drawString("3",54,67);

    g.drawString("0",9,107);
    g.drawString("1",24,107);
    g.drawString("2",39,107);
    g.drawString("3",54,107);

    g.drawString("0",9,147);
    g.drawString("1",24,147);
    g.drawString("2",39,147);
    g.drawString("3",54,147);

    g.drawString("0",9,187);
    g.drawString("1",24,187);
    g.drawString("2",39,187);
    g.drawString("3",54,187);

    //Receiver Entry

    g.drawString("0",359,67);
    g.drawString("1",374,67);
    g.drawString("2",389,67);
    g.drawString("3",404,67);

    g.drawString("0",359,107);
    g.drawString("1",374,107);
    g.drawString("2",389,107);
    g.drawString("3",404,107);

    g.drawString("0",359,147);
    g.drawString("1",374,147);
    g.drawString("2",389,147);
    g.drawString("3",404,147);

    g.drawString("0",359,187);
    g.drawString("1",374,187);
    g.drawString("2",389,187);
    g.drawString("3",404,187);

    g.drawString("S,Sf",5,45);
    g.drawString("Sl",20,85);

    g.drawString("Rf,Rl",404,205);
   
    }


}



--------------------------OUTPUT-------------------------

Assuming Size of the Frame is 2




To find class, subnet mask, first and last IP of the block in which given IP is present

/* To find class, subnet mask, first and last IP of the block in which given IP is present */

import java.io.*;

class Ipfind
{
                public static void main(String[] args)throws IOException {
                               
                                DataInputStream dis = new DataInputStream(System.in);
                                System.out.println("Enter IP Address (eg: 192.168.1.1)");
                                String ipAddr = dis.readLine();
                                String[] ipAddrParts=ipAddr.split("\\.");
                                String mask="";

                                int firstoctet = Integer.parseInt(ipAddrParts[0]);
                                if(firstoctet<=127)
                                {
                                                mask = "255.0.0.0";
                                                System.out.println("Class A IP Address");
                                                System.out.println("The Subnet mask is: "+mask);
                                }
                                else if(firstoctet>=128 && firstoctet<=191)
                                {
                                                mask = "255.255.0.0";
                                                System.out.println("Class B IP Address");
                                                System.out.println("The Subnet mask is: "+mask);
                                }
                                else if(firstoctet>=192 && firstoctet<=223)
                                {
                                                mask = "255.255.255.0";
                                                System.out.println("Class C IP Address");
                                                System.out.println("The Subnet mask is: "+mask);
                                }
                                else if(firstoctet>=224 && firstoctet<=239)
                                {
                                                mask = "255.0.0.0";
                                                System.out.println("Class D IP Address; Used for multicasting");
                                }
                                else if(firstoctet>=240 && firstoctet<=254)
                                {
                                                mask = "255.0.0.0";
                                                System.out.println("Class D IP Address; Experimental Use");
                                }

                               

                                String[] maskParts=mask.split("\\.");
                                String firstAddr="";
                                String lastAddr="";

                                for(int i=0;i<4;i++){
                int x=Integer.parseInt(ipAddrParts[i]);
                int y=Integer.parseInt(maskParts[i]);
                int z=x&y;
                int w=z|(y^255);               //last ip = ipaddress && subnetmask + ~subnetmask
                firstAddr+=z+".";
                lastAddr+=w+".";
            }

        System.out.println("First IP of block: "+firstAddr);
        System.out.println("Last IP of block: "+lastAddr);

                }
}

OUTPUT:

C:\Documents and Settings\students\My Documents\Downloads>java Ipfind
Enter IP Address (eg: 192.168.1.1)
10.0.6.162
Class A IP Address
The Subnet mask is: 255.0.0.0
First IP of block: 10.0.0.0.

Last IP of block: 10.255.255.255.

PROGRAM FOR HAMMING CODE GENERATION AND DETECTION


/*PROGRAM FOR HAMMING CODE GENERATION AND DETECTION */


package program;
import java.util.*;
class code
{
    Scanner sc=new Scanner(System.in);
    int a[]=new int[10];
    int d[]=new int[10];
    int n,i,ch,j,x;
    double r,r1;

    void get()
    {
        System.out.println("\nEnter the Number of parity bits");
        n=sc.nextInt();
        r1=Math.pow(2,n);
        x=(int)r1;
    }
    void generate()
    {
        System.out.println("Enter the Four bits");
        for(i=0;i<4;i++)
            a[i]=sc.nextInt();
        d[7]=a[0];
        d[6]=a[1];
        d[5]=a[2];
        d[3]=a[3];
        d[1]=(d[3]+d[5]+d[7])%2;
        d[2]=(d[3]+d[6]+d[7])%2;
        d[4]=(d[5]+d[6]+d[7])%2;

    }
    void detect()
    {
              System.out.println("Enter the Seven bit Number");
              for(i=x-1;i>=1;i--)
              {
                  a[i]=sc.nextInt();
              }
        d[7]=a[7];
        d[6]=a[6];
        d[5]=a[5];
        d[3]=a[3];
        d[1]=(a[3]+a[5]+a[7])%2;
        d[2]=(a[3]+a[6]+a[7])%2;
        d[4]=(a[5]+a[6]+a[7])%2;

        for(i=0;i<n;i++)
        {
            r=Math.pow(2,i);
            j=(int)r;
            if(a[j]==d[j])
            {
                System.out.println("No Error at "+j+ "Position\n");
            }
           if(a[j]!=d[j])
            {
                System.out.println("The Error Occured at "+j+ "Position\n");

            }
        }
    }
    void display()
    {
        for(i=x-1;i>=1;i--)
        {
            System.out.print( d[i]);
        }
    }
}
public class Hamming
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int ch;
        code c=new code();
     do
       {
        System.out.println("\n1.Hamming code Generation\n2.Error Detection\n3.Exit\n");
        ch=sc.nextInt();
      
        switch(ch)
          {
           case 1:
                c.get();
                c.generate();
                c.display();
                break;
            case 2:
                c.get();
                c.detect();
                c.display();
                 break;
            case 3:

        }
     }while(ch!=3);
    }

}