Академический Документы
Профессиональный Документы
Культура Документы
LTPC
0032
Hamming code
Algorithm :
The following general algorithm generates a single-error correcting (SEC) code for
any number of bits.
1. Number the bits starting from 1: bit 1, 2, 3, 4, 5, etc.
2. Write the bit numbers in binary: 1, 10, 11, 100, 101, etc.
3. All bit positions that are powers of two (have only one 1 bit in the binary form
of their position) are parity bits: 1, 2, 4, 8, etc. (1, 10, 100, 1000)
4. All other bit positions, with two or more 1 bits in the binary form of their
position, are data bits.
5. Each data bit is included in a unique set of 2 or more parity bits, as
determined by the binary form of its bit position.
1. Parity bit 1 covers all bit positions which have the least significant bit
set: bit 1 (the parity bit itself), 3, 5, 7, 9, etc.
2. Parity bit 2 covers all bit positions which have the second least
significant bit set: bit 2 (the parity bit itself), 3, 6, 7, 10, 11, etc.
3. Parity bit 4 covers all bit positions which have the third least significant
bit set: bits 47, 1215, 2023, etc.
4. Parity bit 8 covers all bit positions which have the fourth least
significant bit set: bits 815, 2431, 4047, etc.
5. In general each parity bit covers all bits where the bitwise AND of the
parity position and the bit position is non-zero.
The form of the parity is irrelevant. Even parity is simpler from the perspective of
theoretical mathematics, but there is no difference in practice.
}
int p1[]={h[1],h[3],h[5],h[7]};
int p2[]={h[2],h[3],h[6],h[7]};
int p3[]={h[4],h[5],h[6],h[7]};
int parity[]=new int[3];
/************Setting the value of parity bit*************/
parity[0]=set_parity_bit(p1);
parity[1]=set_parity_bit(p2);
parity[2]=set_parity_bit(p3);
/************Inserting the parity bits in the hamming code**********/
h[1]=parity[0];
h[2]=parity[1];
h[4]=parity[2];
System.out.println("\nSENDER:");
System.out.print("\nThe data bits entered are: ");
for(int i=3;i>=0;--i)
System.out.print(d[i]+" ");
System.out.println("\nThe Parity bits are: ");
for(int i=2;i>=0;--i)
System.out.println("Value of P"+(i+1)+" is "+parity[i]+" ");
System.out.print("\nThe Hamming code is as follows :-\nD4 D3 D2 P3 D1 P2
P1 \n");
for(int i=(n+k);i>0;--i)
System.out.print(h[i]+" ");
System.out.println();
System.out.println("\nEnter the hamming code with error at any position of
your choice.\nNOTE: ENTER A SPACE AFTER EVERY BIT POSITION.\nError
should be present only at one bit position");
for(int i=7;i>0;--i)
h[i]=scr.nextInt();
int p4[]={h[1],h[3],h[5],h[7]};
int p5[]={h[2],h[3],h[6],h[7]};
int p6[]={h[4],h[5],h[6],h[7]};
parity[0]=set_parity_bit(p4);
parity[1]=set_parity_bit(p5);
parity[2]=set_parity_bit(p6);
int
position=(int)
(parity[2]*Math.pow(2,2)+parity[1]*Math.pow(2,1)+parity[0]*Math.pow(2,0));
System.out.println("\nRECEIVER:");
System.out.println("Error is detected at position "+position+" at the receiving
end.");
System.out.println("Correcting the error.... ");
if(h[position]==1)
h[position]=0;
else
h[position]=1;
System.out.print("The correct code is ");
for(int i=7;i>0;--i)
System.out.print(h[i]+" ");
}}
/* Output
C:\Users\Aditya\Desktop>java hamming_code
This is hamming code error detection and correction using EVEN parity
Enter 4 data bits.D4 D3 D2 D1
Enter the value of D4
1
Enter the value of D3
0
Enter the value of D2
1
Enter the value of D1
0
3 parity bits are required for the transmission of data bits.
SENDER:
The data bits entered are: 1 0 1 0
The Parity bits are:
Value of P3 is 0
Value of P2 is 1
Value of P1 is 0
The Hamming code is as follows :D4 D3 D2 P3 D1 P2 P1
1010010
Enter the hamming code with error at any position of your choice.
NOTE: ENTER A SPACE AFTER EVERY BIT POSITION.
Error should be present only at one bit position
1010110
RECEIVER:
Error is detected at position 3 at the receiving end.
Correcting the error....
The correct code is 1 0 1 0 0 1 0
*/
Algorithm Rough algorithm of the sliding window protocols:
1. Transmit all frames in the senders window (no more than from SL to SU1)
2. Whenever the receiver gets a frame in its window:
(a) it generates an ACK for the highest frame correctly received (same as the frame
for protocol 5).
(b) if the frame RL has been received it passes RL to the host and bumps RL and RU
(advances the window).
3. Whenever the receiver gets a damaged frame or a frame not within its window it generates
a NAK for one less than the frame expected (RL 1) (only for protocol 6).
4. Whenever the sender receives an ACK for a frame within its window, it marks that frame
as having been correctly sent and received. If SL is ACKed then increment SL and SU
(advance the senders window) and transmit SU1 (last previously unsent frame).
5. Whenever a timer goes off, retransmit the corresponding frame.
}
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 evryone
Acknowledgment sent
out.flush();
in=new ObjectInputStream(sender.getInputStream());
str=(String)in.readObject();
System.out.println("reciver > "+str);
System.out.println("Enter the data to send....");
packet=br.readLine();
n=packet.length();
do{
try{
if(i<n){
msg=String.valueOf(sequence);
msg=msg.concat(packet.substring(i,i+1));
}
else if(i==n){
msg="end";out.writeObject(msg);break;
}
out.writeObject(msg);
sequence=(sequence==0)?1:0;
out.flush();
System.out.println("data sent>"+msg);
ack=(String)in.readObject();
System.out.println("waiting for ack.....\n\n");
if(ack.equals(String.valueOf(sequence))){
i++;
System.out.println("receiver > "+" packet recieved\n\n");
}
else{
System.out.println("Time out resending data....\n\n");
sequence=(sequence==0)?1:0;
}
}catch(Exception e){}
}while(i<n+1);
System.out.println("All data sent. exiting.");
}catch(Exception e){}
finally{
try{
in.close();
out.close();
sender.close();
}
catch(Exception e){}
}
}
public static void main(String args[]){
Sender s=new Sender();
s.run();
}
}
//RECEIVER PROGRAM
import java.io.*;
import java.net.*;
public class Reciever{
ServerSocket reciever;
Socket connection=null;
ObjectOutputStream out;
ObjectInputStream in;
String packet,ack,data="";
int i=0,sequence=0;
Reciever(){}
public void run(){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
reciever = new ServerSocket(2004,10);
System.out.println("waiting for connection...");
connection=reciever.accept();
sequence=0;
System.out.println("Connection established :");
out=new ObjectOutputStream(connection.getOutputStream());
out.flush();
in=new ObjectInputStream(connection.getInputStream());
out.writeObject("connected .");
do{
try{
packet=(String)in.readObject();
if(Integer.valueOf(packet.substring(0,1))==sequence){
data+=packet.substring(1);
sequence=(sequence==0)?1:0;
System.out.println("\n\nreceiver
>"+packet);
}
else
{
System.out.println("\n\nreceiver
>"+packet +" duplicate data");
}
if(i<3){
out.writeObject(String.valueOf(sequence));i++;
}
else{
out.writeObject(String.valueOf((sequence+1)%2));
i=0;
}
}
catch(Exception e){}
}while(!packet.equals("end"));
System.out.println("Data recived="+data);
out.writeObject("connection ended .");
}
catch(Exception e){}
finally{
try{
in.close();
out.close();
reciever.close();
}
catch(Exception e){}
}
}
public static void main(String args[]){
Reciever s=new Reciever();
while(true){
s.run();
}
}
}
OUTPUT:
//SENDER OUTPUT
Waiting for Connection....
reciver > connected .
Enter the data to send....
myname
data sent>0m
waiting for ack.....
receiver
> packet recieved
data sent>1y
waiting for ack.....
receiver
> packet recieved
data sent>0n
waiting for ack.....
receiver
> packet recieved
data sent>1a
waiting for ack.....
Time out resending data....
data sent>1a
waiting for ack.....
receiver
> packet recieved
data sent>0m
waiting for ack.....
receiver
> packet recieved
data sent>1e
waiting for ack.....
receiver
> packet recieved
All data sent. exiting.
//RECEIVER OUTPUT
waiting for connection...
Connection established :
receiver
>0m
receiver
>1y
receiver
>0n
receiver
>1a
receiver
>1a duplicate data
receiver
>0m
receiver
>1e
Data recived=myname
waiting for connection...
Go Back N (Java)
1. Start.
2. Establish connection (recommended UDP)
3. Accept the window size from the client(should be <=40)
4. Accept the packets from the network layer.
5. Calculate the total frames/windows required.
6. Send the details to the client(totalpackets,totalframes.)
7. Initialise the transmit buffer.
8. Built the frame/window depending on the windowsize.
9. Transmit the frame.
10. Wait for the acknowledgement frame.
11. Check for the acknowledgement of each packet and repeat the process
from the packet for which the first negative acknowledgement is
received. Else continue as usual.
12. Increment the framecount and repeat steps 7 to 12 until all packets are
transmitted.
13. Close the connection.
14. Stop.
Go Back N (Java)
Problem Definition: Write a program in Java to implement Go Back N
algorithm.
The Program sends the frames from the Client to the Server with checking for
missing frames via sending an acknowledgement.
P.S. Enter the inputs in the Client program after the connection is established
with the Server.
/*Server Program*/
import java.net.*;
import java.io.*;
import java.util.*;
public class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket server=new ServerSocket(6262);
System.out.println(Server established.);
Socket client=server.accept();
ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
ObjectInputStream ois=new ObjectInputStream(client.getInputStream());
System.out.println(Client is now connected.);
int x=(Integer)ois.readObject();
int k=(Integer)ois.readObject();
int j=0;
int i=(Integer)ois.readObject();
boolean flag=true;
Random r=new Random(6);
int mod=r.nextInt(6);
while(mod==1||mod==0)
mod=r.nextInt(6);
while(true)
{
int c=k;
for(int h=0;h<=x;h++)
{
System.out.print(|+c+|);
c=(c+1)%x;
}
System.out.println();
System.out.println();
if(k==j)
{
System.out.println(Frame +k+ recieved+\n+Data:+j);
j++;
System.out.println();
}
else
System.out.println(Frames recieved not in correct order+\n+ Expected
farme: + j +\n+ Recieved frame no :+ k);
System.out.println();
if(j%mod==0 && flag)
{
System.out.println(Error found. Acknowledgement not sent. );
flag=!flag;
j;
}
else if(k==j-1)
{
oos.writeObject(k);
System.out.println(Acknowledgement sent);
}
System.out.println();
if(j%mod==0)
flag=!flag;
k=(Integer)ois.readObject();
if(k==-1)
break;
i=(Integer)ois.readObject();
}
System.out.println(Client finished sending data. Exiting);
oos.writeObject(-1);
}
}
/*Client Program*/
import java.util.*;
import java.net.*;
import java.io.*;
public class Client
{
public static void main(String args[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print(Enter the value of m : );
int m=Integer.parseInt(br.readLine());
int x=(int)((Math.pow(2,m))-1);
System.out.print(Enter no. of frames to be sent:);
int count=Integer.parseInt(br.readLine());
int data[]=new int[count];
int h=0;
for(int i=0;i<count;i++)
{
System.out.print(Enter data for frame no +h+ => );
data[i]=Integer.parseInt(br.readLine());
h=(h+1)%x;
}
Socket client=new Socket(localhost,6262);
ObjectInputStream ois=new ObjectInputStream(client.getInputStream());
ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
System.out.println(Connected with server.);
boolean flag=false;
GoBackNListener listener=new GoBackNListener(ois,x);
listener=new GoBackNListener(ois,x);
listener.t.start();
int strt=0;
h=0;
oos.writeObject(x);
do
{
int c=h;
for(int i=h;i<count;i++)
{
System.out.print(|+c+|);
c=(c+1)%x;
}
System.out.println();
System.out.println();
h=strt;
for(int i=strt;i<x;i++)
{
System.out.println(Sending frame:+h);
h=(h+1)%x;
System.out.println();
oos.writeObject(i);
oos.writeObject(data[i]);
Thread.sleep(100);
}
listener.t.join(3500);
if(listener.reply!=x-1)
{
System.out.println(No reply from server in 3.5 seconds. Resending data from
frame no + (listener.reply+1));
System.out.println();
strt=listener.reply+1;
flag=false;
}
else
{
System.out.println(All elements sent successfully. Exiting);
flag=true;
}
}while(!flag);
oos.writeObject(-1);
}
}
class GoBackNListener implements Runnable
{
Thread t;
ObjectInputStream ois;
int reply,x;
GoBackNListener(ObjectInputStream o,int i)
{
t=new Thread(this);
ois=o;
reply=-2;
x=i;
}
@Override
public void run() {
try
{
int temp=0;
while(reply!=-1)
{
reply=(Integer)ois.readObject();
if(reply!=-1 && reply!=temp+1)
reply=temp;
if(reply!=-1)
{
temp=reply;
System.out.println(Acknowledgement of frame no + (reply%x) +
recieved.);
System.out.println();
}
}
reply=temp;
}
catch(Exception e)
{
System.out.println(Exception => + e);
}
}
}
/*Client Output
Enter the value of m : 7
Enter no. of frames to be sent:5
Enter data for frame no 0 => 1
Enter data for frame no 1 => 2
Enter data for frame no 2 => 3
Enter data for frame no 3 => 4
Enter data for frame no 4 => 5
Connected with server.
|0||1||2||3||4|
Sending frame:0
Acknowledgement of frame no 0 recieved.
Sending frame:1
Sending frame:2
Sending frame:3
Sending frame:4
Sending frame:5
*/
/*Server Output
Server established.
Client is now connected.
|0||1||2||3||4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||19||20||21||22||23||24||
25||26||27||28||29||30||31||32||33||34||35||36||37||38||39||40||41||42||43||44||45||46||
47||48||49||50||51||52||53||54||55||56||57||58||59||60||61||62||63||64||65||66||67||68||
69||70||71||72||73||74||75||76||77||78||79||80||81||82||83||84||85||86||87||88||89||90||
91||92||93||94||95||96||97||98||99||100||101||102||103||104||105||106||107||108||109||
110||111||112||113||114||115||116||117||118||119||120||121||122||123||124||125||126||
0|
Frame 0 recieved
Data:0
Acknowledgement sent
|1||2||3||4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||19||20||21||22||23||24||25||
26||27||28||29||30||31||32||33||34||35||36||37||38||39||40||41||42||43||44||45||46||47||
48||49||50||51||52||53||54||55||56||57||58||59||60||61||62||63||64||65||66||67||68||69||
70||71||72||73||74||75||76||77||78||79||80||81||82||83||84||85||86||87||88||89||90||91||
92||93||94||95||96||97||98||99||100||101||102||103||104||105||106||107||108||109||
110||111||112||113||114||115||116||117||118||119||120||121||122||123||124||125||126||
0||1|
Frame 1 recieved
Data:1
Error found. Acknowledgement not sent.
|2||3||4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||19||20||21||22||23||24||25||26||
27||28||29||30||31||32||33||34||35||36||37||38||39||40||41||42||43||44||45||46||47||48||
49||50||51||52||53||54||55||56||57||58||59||60||61||62||63||64||65||66||67||68||69||70||
71||72||73||74||75||76||77||78||79||80||81||82||83||84||85||86||87||88||89||90||91||92||
93||94||95||96||97||98||99||100||101||102||103||104||105||106||107||108||109||110||
111||112||113||114||115||116||117||118||119||120||121||122||123||124||125||126||0||1||
2|
Frames recieved not in correct order
Expected farme:1
Recieved frame no :2
|3||4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||19||20||21||22||23||24||25||26||
27||28||29||30||31||32||33||34||35||36||37||38||39||40||41||42||43||44||45||46||47||48||
49||50||51||52||53||54||55||56||57||58||59||60||61||62||63||64||65||66||67||68||69||70||
71||72||73||74||75||76||77||78||79||80||81||82||83||84||85||86||87||88||89||90||91||92||
93||94||95||96||97||98||99||100||101||102||103||104||105||106||107||108||109||110||
111||112||113||114||115||116||117||118||119||120||121||122||123||124||125||126||0||1||
2||3|
Frames recieved not in correct order
Expected farme:1
Recieved frame no :3
|4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||19||20||21||22||23||24||25||26||27||
28||29||30||31||32||33||34||35||36||37||38||39||40||41||42||43||44||45||46||47||48||49||
50||51||52||53||54||55||56||57||58||59||60||61||62||63||64||65||66||67||68||69||70||71||
72||73||74||75||76||77||78||79||80||81||82||83||84||85||86||87||88||89||90||91||92||93||
94||95||96||97||98||99||100||101||102||103||104||105||106||107||108||109||110||111||
112||113||114||115||116||117||118||119||120||121||122||123||124||125||126||0||1||2||3||
4|
Frames recieved not in correct order
Expected farme:1
Recieved frame no :4
*/
ns
[new
color
1
[open
out.nam
finish
{}
ns
Simulator]
Blue
w]
{
nf
flush-trace
close
exec
exit
}
nam
set
n0
set
n1
$ns
at
0.0
$n0
$ns at 0.0 $n1 label \receiver\
$nf
&
0
out.nam
label
$ns
duplex-link
$n0
$n1
$ns duplex-link-op $n0 $n1 orient right
[$ns
[$ns
\
sender
1Mb
200ms
node]
node]
DropTail
set
tcp
[new
$ns
attach-agent
$n0
$tcp
set
fid_
$tcp
set
window_
$tcp
set
maxcwnd_
$ns
add-agent-trace
$tcp
$ns
monitor-agent-trace
set
tcpsink
[new
$ns attach-agent $n1 $tcpsink
Agent/TCP]
$tcp
1
1
1
tcp
$tcp
Agent/TCPSink]
$ns
connect
set
ftp
$ftp attach-agent $tcp
$tcpsink
Application/FTP]
$ns
$ns at
$ns
$ns
$ns
$ns
$ns
$ns
$ns
$ns
$ns run
$tcp
[new
at
0.5
$ftp
3.0 $ns detach-agent $n0 $tcp ; $ns detach-agent
at
1.0
$ns
trace-annotate
\send
at
1.4
$ns
trace-annotate
\recieve
at
2.0
$ns
trace-annotate
\send
at
2.5
$ns
trace-annotate
\receive
at
3.2
$ns
trace-annotate
\send
at
3.5
$ns
trace-annotate
\receive
at
3.8
$ns
trace-annotate
\send
at
4.0
start
$n1 $tcpsink
packet
1\
ack
1\
packet
2\
ack
2\
packet
3\
ack
3\
packet
4\
finish