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);
    }

}



Wednesday, 20 August 2014

CRC code genration and Error Detection

import java.io.*;
class crc_gen
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int[] data;
        int[] div;
        int[] divisor;
        int[] rem;
        int[] crc;
        int data_bits, divisor_bits, tot_length;
       
        System.out.println("Enter number of data bits : ");
        data_bits=Integer.parseInt(br.readLine());
        data=new int[data_bits];

        System.out.println("Enter data bits : ");
        for(int i=0; i<data_bits; i++)
            data[i]=Integer.parseInt(br.readLine());

        System.out.println("Enter number of bits in divisor : ");
        divisor_bits=Integer.parseInt(br.readLine());
        divisor=new int[divisor_bits];
       
        System.out.println("Enter Divisor bits : ");
        for(int i=0; i<divisor_bits; i++)
            divisor[i]=Integer.parseInt(br.readLine());


/*        System.out.print("Data bits are : ");
        for(int i=0; i< data_bits; i++)
            System.out.print(data[i]);      
        System.out.println();
       
        System.out.print("divisor bits are : ");
        for(int i=0; i< divisor_bits; i++)
            System.out.print(divisor[i]);      
        System.out.println();
       
*/        tot_length=data_bits+divisor_bits-1;
       
        div=new int[tot_length];
        rem=new int[tot_length];
        crc=new int[tot_length];
    /*------------------ CRC GENERATION-----------------------*/  
        for(int i=0;i<data.length;i++)
            div[i]=data[i];
       
        System.out.print("Dividend (after appending 0's) are : ");
        for(int i=0; i< div.length; i++)
            System.out.print(div[i]);      
        System.out.println();
       
        for(int j=0; j<div.length; j++){
              rem[j] = div[j];
        }
   
        rem=divide(div, divisor, rem);
       
        for(int i=0;i<div.length;i++)           //append dividend and ramainder
        {
            crc[i]=(div[i]^rem[i]);
        }
       
        System.out.println();
        System.out.println("CRC code : ");  
        for(int i=0;i<crc.length;i++)
            System.out.print(crc[i]);
           
    /*-------------------ERROR DETECTION---------------------*/  
        System.out.println();
        System.out.println("Enter CRC code of "+tot_length+" bits : ");
        for(int i=0; i<crc.length; i++)
            crc[i]=Integer.parseInt(br.readLine());
       
       
/*        System.out.print("crc bits are : ");
        for(int i=0; i< crc.length; i++)
            System.out.print(crc[i]);      
        System.out.println();
*/      
        for(int j=0; j<crc.length; j++){
              rem[j] = crc[j];
        }
   
        rem=divide(crc, divisor, rem);
       
        for(int i=0; i< rem.length; i++)
        {
            if(rem[i]!=0)
            {
                System.out.println("Error");
                break;
            }
            if(i==rem.length-1)
                System.out.println("No Error");
        }
       
        System.out.println("THANK YOU.... :)");
    }
   
    static int[] divide(int div[],int divisor[], int rem[])
    {
        int cur=0;
        while(true)
        {
            for(int i=0;i<divisor.length;i++)
                rem[cur+i]=(rem[cur+i]^divisor[i]);
           
            while(rem[cur]==0 && cur!=rem.length-1)
                cur++;
   
            if((rem.length-cur)<divisor.length)
                break;
        }
        return rem;
    }
}



*/OUTPUT :
Enter number of data bits :
7
Enter data bits :
1
0
1
1
0
0
1
Enter number of bits in divisor :
3
Enter Divisor bits :
1
0
1
Data bits are : 1011001
divisor bits are : 101
Dividend (after appending 0's) are : 101100100

CRC code :
101100111
Enter CRC code of 9 bits :
1
0
1
1
0
0
1
0
1
crc bits are : 101100101
Error
THANK YOU.... :)
Press any key to continue.../*

Sunday, 29 June 2014

Program For Hamming code generation,Error detection and correction:----------


#include<iostream.h>
#include<conio.h>

void main()
{
int data[7], c0, c1, c2, op,i, loc;
char a[4], b[7];
clrscr();
do
{
printf("\n 1. Hamming code generation");
printf("\n 2. Error detection and correction");\
printf("\n 3. Quit");
printf("\n 4. Enter choice :-");
scanf("%d", &op);
switch(op)
{
case 1:
printf("\n Enter a 4 bit data");
scanf("%s", a)
for(i=0; i<7;i++)
{
data[i]=0;
}
data[0]=a[0]-'0';
data[1]=a[1]-'0';
data[2]=a[2]-'0';
data[4]=a[3]-'0';
data[6]=(data[0]+data[2]+data[4])%2;
data[5]=(data[4]+data[1]+data[0])%2;
data[3]=(data[0]+data[1]+data[2])%2;
printf("\n data as encoded with hamming parity bits :\n");
printf("\n d3   d2   d1   p2   d0   p1   p0");
printf("\n");
for(i=0; i<7; i++)
{
printf("%d", data[i]);
}
break;
case 2:
printf("\nEnter a 7 bit hamming coded data :")
scanf("%s", b);
for(i=0;i<7;i++)
data[i]=b[i]-'0';
c0=(data[6]+data[0]+data[2]+data[4])%2;
c1=(data[5]+data[4]+data[1]+data[0])%2;
c2=(data[3]+data[0]+data[1]+data[2])%2;
loc=7-(4*c2+2*c1+c0);
if(loc==0)
printf("\n No Error");
else
{
printf("\nError at the location = %d", loc);
printf("\nCorrected data :");
data[loc]=(!data[loc]);
printf("\n");
for(i=0;i<7;i++)
printf("%d", data[i]);
}
break;
}
}while(op!=3);
}
Program For BOOT's Algorithm in C:--------


#include<stdio.h>
#include<conio.h>
#include<string.h>

int q1=0,n=0,count=0,Q[4],A[4],M[4],M2[4];
char b1[4],b2[4];

void input()
{
printf("\nEnter first 4-bit binary number: ");
gets(b1);
printf("\nEnter second 4-bit binary number: ");
gets(b2);
}

void initialize()
{
int a,b,i;
for(i=3;i>=0;i--)
{
a=(int)b1%10;
M[i]=a;
b1[i]/=10;
b=(int)b2%10;
Q[i]=b;
b2[i]/=10;
A[i]=0;
}
}

void add()
{
int i,a,b,c=0;
int sum[4];
for(i=3;i>=0;i--)
{
a=A[i];
b=M[i];
if((a+b+c)==0)
{
sum[i]=0;
c=0;
}
else if((a+b+c)==1)
{
sum[i]=1;
c=0;
}
else if((a+b+c)==2)
{
sum[i]=0;
c=1;
}
else if((a+b+c)==3)
{
sum[i]=1;
c=1;
}
}
for(i=0;i<4;i++)
{
A[i]=sum[i];
}
}

void twosComplement()
{
int i,b,a;
int flag=0;
for(i=0;i<n;i++)
{
if(M[i]==0) M2[i]=1;
else if(M[i]==1) M2[i]=0;
}
i=3;
while(flag==0)
{
b=M2[i];
flag=1;
if(a==1)
{
M2[i]=1;
flag=1;
break;
}
else if(a==2)
{
M2[i]=0;
i--;
}
}
}

void sub()
{
int i,a,b,c=0;
int sum[4];
twosComplement();
for(i=3;i>=0;i++)
{
a=A[i];
b=M2[i];
if((a+b+c)==0)
{
sum[i]=0;
c=0;
}
else if((a+b+c)==1)
{
sum[i]=1;
c=0;
}
else if((a+b+c)==2)
{
sum[i]=0;
c=1;
}
else if((a+b+c)==3)
{
sum[i]=1;
c=1;
}
}
for(i=0;i<n;i++) A[i]=sum[i];
}

void shift()
{
int i;
q1=Q[3];
for(i=3;i>0;i--)
{
Q[i]=Q[i-1];
}
Q[0]=A[3];
for(i=3;i>0;i--)
{
A[i]=A[i-1];
}
A[0]=A[i];
}

void display()
{
int i;
for(i=0;i<n;i++) printf("%d\t",A[i]);
for(i=0;i<n;i++) printf("%d\t%d\n",Q[i],q1);
}

void booth()
{
int i;
while(count!=0)
{
if(Q[3]==0 && q1==1)
{
add();
display();
}
else if(Q[3]==1 && q1==0)
{
sub();
display();
}
shift();
display();
printf("\nBooth's Algorithm: ");
}
printf("\nThe answer is: ");
for(i=0;i<n;i++) printf("%d",A[i]);
for(i=0;i<n;i++) printf("%d",Q[i]);
}

void main()
{
clrscr();
input();
initialize();
printf("\nA\tQ\tQ-1");
booth();
display();
printf("\n\n");

getch();
}