1.
IMPLEMENTATION OF ERROR DETECTION AND CORRECTION TECHNIQUE
AIM:
To write a ‘C’ program to implement Error Detection and Correction Techniques.
ALGORITHM:
1. Get four bit data
2. Generate the generator matrix
3. Encode the data
4. Create parity check matrix
5. Enter the error data
6. Calculate syndrome using e.HT
7. Decode the error data.
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:
1. Create a server socket.
2. Assume the sending window size
3. If the receiver is ready initialize sender’s frame sequence to 0
4. Get data from user
5. Send it to the receiver along with sequence number
6. Increment sequence number by 1
7. Repeat step 4-6 until all frames have been sent
8. Wait for acknowledgement
9. If all ACK have arrived then go to step 11.
10. Set sequence number to earliest outstanding frame for which there is no ACK. Go to step 4
11. Stop
CLIENT:
1. Create a socket for client
2. Indicate to sender, the readiness to accept frames
3. Initialize receiver’s expected frame sequence to 0
4. Accept the incoming frame
5. If frame’s sequence == receiver’s sequence and go to step 6.
6. Send an ACK.
7. Repeat step 3-6 until all frames are received in sequence and go to step 8.
8. Discard frame, thereby force the sender to retransmit. Go to step 3.
9. Stop.
SENDER:
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:
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();
RESULT: Thus the program for sliding window protocol was implemented successfully.
2.b.IMPLEMENTATION OF STOP & WAIT PROTOCOL
AIM
To implement the stop and wait protocol using Java .
PROCEDURE
Sender
Step1: sequence „² 0
Step2: Accept new packet and assign sequence to it.
Step3: Send packet sequence with sequence number sequence.
Step4: Set timer for recently sent packets.
Step5: If error free acknowledgment from receiver and NextFrameExpected -> sequence
then sequence,NextFrameExpected.
Step6: If time out then go to step3.
Step7: Stop.
Receiver
Step1: Start.
Step2: NextFrameExpecteds 0, repeat steps 3 forever.
Step3: If error-free frame received and sequence= NextFrameExpected, then pass packet to higher
layer and NextFrameExpecteds NextFrameExpected+1(modulo 2).
Step4: Stop.
Sender.java
import java.io.*;
import java.net.*;
public class Sender{
Socket sender;
ObjectOutputStream out;
ObjectInputStream in;
String packet,ack,str, msg;
int n,i=0,sequence=0;
Sender(){}
public void run(){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Waiting for Connection....");
sender = new Socket("localhost",2005);
sequence=0;
out=new ObjectOutputStream(sender.getOutputStream());
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.java
import java.io.*;
import java.net.*;
public class Receiver{
ServerSocket reciever;
Socket connection=null;
ObjectOutputStream out;
ObjectInputStream in;
String packet,ack,data="";
int i=0,sequence=0;
Receiver(){}
public void run(){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
reciever = new ServerSocket(2005,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[]){
Receiver s=new Receiver();
while(true){
s.run();
}
RESULT:
Thus the program is executed successfully and the output is verified.
IMPLEMENTATION OF HIGH LEVEL DATA LINK CONTROL
AIM:
To write a ‘C’ program to implement High Level Data Link Control.
ALGORITHM:
1. Initialize the variables.
2. Enter the string of bits.
3. Insert a flag value.
4. Insert a zero at the beginning and end of each flag value
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
int i,j,count=0,nl;
char str[100];
printf("enter the bit string:");
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:
ENTER THE BIT STRING: 000111111001
000111110001
PING COMMAND PROGRAM USING JAVA
AIM:
To write a Java program to execute the PING command.
ALGORITHM:
1. Start the process.
2. Establish the connection between two systems
3. Get the address of the system to be pinged
4. Print the number of packets sent and received
5. Stop the process.
Program
import java.io.*;
import java.net.*;
class pingTest {
public static void main(String[] args) {
String ip = "127.0.0.1";
String pingResult = "";
String pingCmd = "ping " + ip;
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec(pingCmd);
BufferedReader in = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
pingResult += inputLine;
in.close();
} catch (IOException e) {
System.out.println(e);
Output:
Result:
Thus the PING command program was executed successfully.
Traceroute COMMAND PROGRAM USING JAVA
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 :
Tracing route to www.google.com [74.125.28.106]
over a maximum of 30 hops:
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.
NsLookUP COMMAND PROGRAM USING JAVA
import java.net.*;
import java.io.*;
public class Program
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
if(args.length > 0 ){ //use command line
for(int i=0; i<args.length; i++){
System.out.println(lookup(args[i]));
}else{
BufferedReader in = new BufferedReader(new InputStreamReader (System.in));
System.out.println("Enter hostname or IP addresse.Enter \"exit\" to quit");
try{
while(true){
String host = in.readLine();
if(host.equalsIgnoreCase("exit")||host.equalsIgnoreCase("quit")){
break;
System.out.println(lookup(host));
}catch(IOException e){
System.err.println(e);
} /*end main */
private static String lookup(String host){
InetAddress node;
//get the bytes of the address
try{
node = InetAddress.getByName(host);
}catch(UnknownHostException e){
return "Cannot resolve host " + host;
if(isHostname(host)){
return node.getHostAddress();
}else {
//this is an IP address
return node.getHostName();
} // end of lookup
private static boolean isHostname(String host) {
//is this a ipv6 address
if(host.indexOf(":") != -1) return false;
char[] ca = host.toCharArray();
//if we see a character that is neither a digit nor a period
//then host is probably a hostname
for(int i=0; i<ca.length; i++){
if(!Character.isDigit(ca[i])){
if(ca[i] != '.') return true;
//Everything was either a digit or a period
// so host looks like a ip4v address in dotted quad format
return false;
}//end ishostname
}//end nslookupcl
Output:
S:\cn>javac Program.java
S:\cn>java Program
Enter hostname or IP addresse.Enter "exit" to quit
www.google.com
216.58.197.68
exit