Documente Academic
Documente Profesional
Documente Cultură
AIM:
To write a ‘C’ program to implement Error Detection and Correction Techniques.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
char data[5];
int encoded[8],edata[7],syndrome[3];
int hmatrix[3][7] = {
1,0,0,0,1,1,1,
0,1,0,1,0,1,1,
0,0,1,1,1,0,1
};
char gmatrix[4][8]={"0111000","1010100","1100010","1110001"};
int main(){
int i,j;
printf("\nHamming code----- Encoding\n");
printf("Enter 4 bit data : ");
scanf("%s",data);
printf("\nGenerator matrix\n");
for(i=0;i<4;i++)
printf("%s\n",gmatrix[i]);
printf("\nEncoded data ");
for(i=0;i<7;i++)
{
for(j=0;j<4;j++)
encoded[i]+=((data[j]-'0')*(gmatrix[j][i]-'0'));
encoded[i]=encoded[i]%2;
printf("%d ",encoded[i]);
}
printf("\nHamming code----- Decoding\n");
printf("Enter encoded bits as recieved : ");
for(i=0;i<7;i++)
scanf("%d",&edata[i]);
for(i=0;i<3;i++)
{
for(j=0;j<7;j++)
syndrome[i]+=(edata[j]*hmatrix[i][j]);
syndrome[i]=syndrome[i]%2;
}
for(j=0;j<7;j++)
if((syndrome[0]==hmatrix[0][j]) && (syndrome[1]==hmatrix[1][j])&&
(syndrome[2]==hmatrix[2][j]))
break;
if(j==7)
printf("\nError free\n");
else
{
printf("\nError recieved at bit number %d of data\n",j+1);
edata[j]=!edata[j];
printf("\nCorrect data should be : ");
for(i=0;i<7;i++)
printf("%d",edata[i]);
} getch();
return 0;
}
RESULT:
Thus the program for High Level Data Link was implemented successfully.
2.a.IMPLEMENTATION OF SLIDING WINDOW PROTOCOL USING JAVA
AIM:
To write a JAVA program to simulate a sliding window protocol that uses Selective Repeat ARQ.
ALGORITHM:
SERVER:
10. Set sequence number to earliest outstanding frame for which there is no ACK. Go to step 4
11. Stop
CLIENT:
6. Send an ACK.
7. Repeat step 3-6 until all frames are received in sequence and go to step 8.
SENDER:
import java.net.*;
import java.io.*;
import java.rmi.*;
Socket s=ser.accept();
PrintStream p;
int sptr=0,sws=8,nf,ano,i;
String ch;
do
p=new PrintStream(s.getOutputStream());
nf=Integer.parseInt(in.readLine());
p.println(nf);
if(nf<=sws-1)
sbuff[sptr]=in.readLine();
p.println(sbuff[sptr]);
sptr=++sptr%8;
sws-=nf;
System.out.print("Acknowledgment received");
ano=Integer.parseInt(in1.readLine());
sws+=nf;
else
break;
ch=in.readLine(); p.println(ch);
while(ch.equals("yes"));
s.close();
RECEIVER:
import java.net.*;
import java.io.*;
class slidreceiver
int i=0,rptr=-1,nf,rws=8;
do
nf=Integer.parseInt(in.readLine());
if(nf<=rws-1)
for(i=1;i<=nf;i++)
rptr=++rptr%8;
rbuf[rptr]=in.readLine();
rws-=nf;
System.out.println("\nAcknowledgment sent\n");
p.println(rptr+1); rws+=nf; }
else
break;
ch=in.readLine();
RESULT: Thus the program for sliding window protocol was implemented successfully.
2.b.IMPLEMENTATION OF STOP & WAIT PROTOCOL
AIM
PROCEDURE
Sender
Step1: sequence „² 0
Step5: If error free acknowledgment from receiver and NextFrameExpected -> sequence
then sequence,NextFrameExpected.
Step7: Stop.
Receiver
Step1: Start.
Step3: If error-free frame received and sequence= NextFrameExpected, then pass packet to higher
Step4: Stop.
Sender.java
import java.io.*;
import java.net.*;
Socket sender;
ObjectOutputStream out;
ObjectInputStream in;
int n,i=0,sequence=0;
Sender(){}
try{
sequence=0;
out=new ObjectOutputStream(sender.getOutputStream());
out.flush();
in=new ObjectInputStream(sender.getInputStream());
str=(String)in.readObject();
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();
if(ack.equals(String.valueOf(sequence))){
i++;
}else{
sequence=(sequence==0)?1:0;
}}catch(Exception e){}
}while(i<n+1);
}catch(Exception e){}
finally{
try{
in.close();
out.close();
sender.close();
catch(Exception e){}
}}
s.run();
}}
Receiver.java
import java.io.*;
import java.net.*;
ServerSocket reciever;
Socket connection=null;
ObjectOutputStream out;
ObjectInputStream in;
String packet,ack,data="";
int i=0,sequence=0;
Receiver(){}
try{
connection=reciever.accept();
sequence=0;
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
}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);
}catch(Exception e){}
finally{
try{in.close();
out.close();
reciever.close();
catch(Exception e){}
}}
while(true){
s.run();
}
RESULT:
Thus the program is executed successfully and the output is verified.
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
int i,j,count=0,nl;
char str[100];
gets(str);
for(i=0;i<strlen(str);i++)
count=0;
for(j=i;j<=(i+5);j++)
if(str[j]=='1')
count++;
if(count==6)
nl=strlen(str)+2;
for(;nl>=(i+5);nl--)
str[nl]=str[nl-1];
str[i+5]='0';
i=i+7;
}
}
puts(str);
getch();
OUTPUT:
000111110001
AIM:
ALGORITHM:
Program
import java.io.*;
import java.net.*;
class pingTest {
String ip = "127.0.0.1";
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec(pingCmd);
InputStreamReader(p.getInputStream()));
String inputLine;
System.out.println(inputLine);
pingResult += inputLine;
in.close();
} catch (IOException e) {
System.out.println(e);
Output:
Result:
import java.io.*;
import java.net.*;
import java.lang.*;
class Traceroute
{
public static void main(String args[]){
BufferedReader in;
try{
Runtime r = Runtime.getRuntime();
Process p = r.exec("tracert www.google.com");
in = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String line;
if(p==null)
System.out.println("could not connect");
while((line=in.readLine())!=null){
System.out.println(line);
//in.close();
}
}catch(IOException e){
System.out.println(e.toString());
}
}
}
Output :
1 70 ms 55 ms 70 ms 10.228.129.13
2 87 ms 84 ms 10.228.149.14
3 82 ms 85 ms 116.202.226.145
4 95 ms 94 ms 136 ms 10.228.158.82
5 Request timed out.
6 53 ms 55 ms 59 ms 116.202.226.21
7 85 ms 74 ms 82 ms 72.14.205.145
8 76 ms 75 ms 71 ms 72.14.235.69
9 124 ms 114 ms 113 ms 216.239.63.213
10 181 ms 194 ms 159 ms 66.249.95.132
11 285 ms 247 ms 246 ms 209.85.142.51
12 288 ms 282 ms 283 ms 72.14.233.138
13 271 ms 283 ms 274 ms 64.233.174.97
14 Request timed out.
15 269 ms 273 ms 283 ms pc-in-f106.1e100.net
[74.125.28.106]
Trace complete.
import java.net.*;
import java.io.*;
/**
System.out.println(lookup(args[i]));
}else{
try{
while(true){
if(host.equalsIgnoreCase("exit")||host.equalsIgnoreCase("quit")){
break;
System.out.println(lookup(host));
}catch(IOException e){
System.err.println(e);
} /*end main */
try{
node = InetAddress.getByName(host);
}catch(UnknownHostException e){
if(isHostname(host)){
return node.getHostAddress();
}else {
//this is an IP address
return node.getHostName();
} // end of lookup
char[] ca = host.toCharArray();
return false;
}//end ishostname
}//end nslookupcl
Output:
S:\cn>javac Program.java
S:\cn>java Program
www.google.com
216.58.197.68
exit