0% found this document useful (0 votes)
68 views26 pages

Implementation of Checksum

The document describes an implementation of the Selective Repeat protocol for reliable data transmission. It defines data structures for packets and acknowledgements. It then sets up a server socket to receive connections from a client. The server requests the client's window size and uses it to determine how many packets to send in each frame. Packets are sent in frames up to the window size. Negative acknowledgements are used to retransmit lost packets. The process continues sending frames until all packets are delivered reliably.

Uploaded by

mitz11041995
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views26 pages

Implementation of Checksum

The document describes an implementation of the Selective Repeat protocol for reliable data transmission. It defines data structures for packets and acknowledgements. It then sets up a server socket to receive connections from a client. The server requests the client's window size and uses it to determine how many packets to send in each frame. Packets are sent in frames up to the window size. Negative acknowledgements are used to retransmit lost packets. The process continues sending frames until all packets are delivered reliably.

Uploaded by

mitz11041995
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 26

IMPLEMENTATION OF CHECKSUM

#include<stdio.h>
#include<conio.h>
#define MAX 10
#define BIT 8
int data[MAX][BIT];
int n;
void receiver();
void sender();
void main()
{
clrscr();
printf("\n\tCHECKSUM\n\n");
printf("Sender side\n\n");
sender();
printf("\n---------------\nReceiver side");
receiver();
getch();
}
void sender()
{
int i,j;
int carry=0;
printf("Enter number of %d bit data to be transmitted :",BIT);
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter dataword %d : ",(i+1));
for(j=0;j<BIT;j++)
{
if(getche()=='1')
data[i][j]=1;
else
data[i][j]=0;
}
}
for(i=0;i<BIT;i++)
data[n][i]=0;
for(i=0;i<n;i++)
{
carry=0;
for(j=BIT-1;j>=0;j--)
{
if((data[n][j] + data[i][j] + carry)==0)
{
data[n][j]=0;
carry=0;
}
else if((data[n][j] + data[i][j] + carry)==1)
{
data[n][j]=1;

carry=0;
}
else if((data[n][j] + data[i][j] + carry)==2)
{
data[n][j]=0;
carry=1;
}
else
{
data[n][j]=1;
carry=1;
}
}
if(carry==1)
{
for(j=BIT-1;j>=0;j--)
{
if(data[n][j]==0)
{
data[n][j]=1;
break;
}
else
data[n][j]=0;
}
}
}
//1's complement
for(i=0;i<BIT;i++)
if(data[n][i]==0)
data[n][i]=1;
else
data[n][i]=0;
printf("\n\nDataword to be transmitted : \n");
for(i=0;i<=n;i++)
{
printf("\n");
for(j=0;j<BIT;j++)
printf("%d",data[i][j]);
}
printf("\t<-- Checksum\n");
}
void receiver()
{
int i,j;
int carry=0;
int sum=0;
printf("\n\nReceived DATA : \n");
for(i=0;i<=n;i++)
{

printf("\n");
for(j=0;j<BIT;j++)
printf("%d",data[i][j]);
}
printf("\t<-- Checksum\n");
for(i=0;i<n;i++)
{
carry=0;
for(j=BIT-1;j>=0;j--)
{
if((data[n][j] + data[i][j] + carry)==0)
{
data[n][j]=0;
carry=0;
}
else if((data[n][j] + data[i][j] + carry)==1)
{
data[n][j]=1;
carry=0;
}
else if((data[n][j] + data[i][j] + carry)==2)
{
data[n][j]=0;
carry=1;
}
else
{
data[n][j]=1;
carry=1;
}
}
if(carry==1)
{
for(j=BIT-1;j>=0;j--)
{
if(data[n][j]==0)
{
data[n][j]=1;
break;
}
else
data[n][j]=0;
}
}
}
//1's complement
for(i=0;i<BIT;i++)
if(data[n][i]==0)
data[n][i]=1;
else
data[n][i]=0;

printf("\nNew checksum : ");


//checking whether sum is zero or not
for(i=0;i<BIT;i++)
{
printf("%d",data[n][i]);
sum+=data[n][i];
}
if(sum==0)
printf("\nData received successfully");
else
printf("\nERROR in received data");
}
OUTPUT :

IMPLEMENTATION OF CRC
#include<stdio.h>
#include<conio.h>
#define MAX 50
int divisor[4];
int k,n;
void encoder(int [], int []);

void decoder(int []);


void main()
{
int dataword[MAX];
int codeword[MAX];
int i;
clrscr();
printf("\tCRC Program\n");
printf("\nEnter number of bits in the dataword : ");
scanf("%d",&k);
//k specify no. of bits in dataword
printf("Enter the dataword :\n");
for(i=0;i<k;i++)
scanf("%d",&dataword[i]);
n=k+3; //n specifies size of codeword. Assusmes that codeword
will be of k+3 bits.
//divisor must be of size n-k+1 bits.
//In this case, divisor will be of ( k + 3 ) - k + 1 = 4 bits.
printf("Enter 4-bit divisor :\n");
for(i=0;i<4;i++)
scanf("%d",&divisor[i]);
//sender genertaing encoded data
encoder(dataword,codeword);
printf("\nDataword : ");
for(i=0;i<k;i++)
printf("%d",dataword[i]);
printf("\nCodeword : ");
for(i=0;i<n;i++)
printf("%d",codeword[i]);
//receiver decoding the codeword
printf("\n--------------------");
printf("\nReceiver side\n\n");
decoder(codeword);
printf("\n--------------------");
printf("\nIf Receiver get codeword containing error");
codeword[2]=1;
decoder(codeword);
getch();
}
void encoder(int dataword[],int codeword[])
{
int i,j;
int remainder[4]={0,0,0,0};
//creates dataword with 000 fedded at end
int augmented_dw[MAX];
for(i=0;i<k;i++)
augmented_dw[i]=dataword[i];
augmented_dw[k]=0;
augmented_dw[k+1]=0;

augmented_dw[k+2]=0;
//generates codeword
for(i=0;i<4;i++)
remainder[i]=augmented_dw[i];
for(i=0;i<n-4;i++)
if(remainder[0]==0)
{
for(j=0;j<3;j++)
remainder[j]=remainder[j+1];
remainder[3]=augmented_dw[i+4];
}
else
{
for(j=1;j<4;j++)
if(remainder[j]==divisor[j])
remainder[j-1]=0;
else
remainder[j-1]=1;
remainder[j-1]=augmented_dw[i+4];
}
for(i=0;i<k;i++)
codeword[i]=dataword[i];
for(j=1;j<4;j++,i++)
codeword[i]=remainder[j];
}
void decoder(int codeword[])
{
int i,j;
int remainder[4]={0,0,0,0};
printf("\ncodeword received : ");
for(i=0;i<n;i++)
printf("%d",codeword[i]);
//Decodes the codeword
//if final remainder is zero then received data is correct
for(i=0;i<4;i++)
remainder[i]=codeword[i];
for(i=0;i<n-4;i++)
{
if(remainder[0]==0)
{
for(j=0;j<3;j++)
remainder[j]=remainder[j+1];
remainder[3]=codeword[i+4];
}
else
{
for(j=1;j<4;j++)
if(remainder[j]==divisor[j])
remainder[j-1]=0;
else
remainder[j-1]=1;

remainder[j-1]=codeword[i+4];
}
}
if(remainder[1]==0 && remainder[2]==0 && remainder[3]==0)
{
printf("\n\nData received successfully");
printf("\nDataword : ");
for(i=0;i<k;i++)
printf("%d",codeword[i]);
}
else
printf("\n\nError in recieved data");
}
OUTPUT :

Stop And Wait


Program:
# stop and wait protocol in normal situation
# features : labeling, annotation, nam-graph, and window size
monitoring
#Create a simulator object
set ns [new Simulator]
#Open the nam trace file
set nf [open A1-stop-n-wait.nam w]
$ns namtrace-all $nf

#Open the Trace file


set tf [open A1-stop-n-wait.tr w]
$ns trace-all $tf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
puts "running nam..."
exec nam A1-stop-n-wait.nam &
exit 0
}
#Create two nodes
set n0 [$ns node]
set n1 [$ns node]
$ns at 0.0 "$n0 label Sender"
$ns at 0.0 "$n1 label Receiver"
#Create a duplex link between the nodes
$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail
$ns duplex-link-op $n0 $n1 orient right
$ns queue-limit $n0 $n1 10
#Setup A TCP connection:
Agent/TCP set nam_tracevar_ true
# create TCP agent and attach it to node no
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
$tcp set window_ 1
$tcp set maxcwnd_ 1
# create a TCPSink agent which act as traffic sink and attach it to node
n1
set tcpsink [new Agent/TCPSink]
$ns attach-agent $n1 $tcpsink
# connect two agents with each other
$ns connect $tcp $tcpsink
# create FTP agent
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns add-agent-trace $tcp tcp
$ns monitor-agent-trace $tcp
$tcp tracevar cwnd_

$ns at 0.1 "$ftp start"


$ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $tcpsink"
$ns at 3.5 "finish"
$ns at 0.0 "$ns trace-annotate \"Stop and Wait with normal operation\""
$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\""
$ns
$ns
$ns
$ns
$ns
$ns
$ns
$ns
$ns
$ns
$ns
$ns
$ns
$ns
$ns

at
at
at
at
at
at
at
at
at
at
at
at
at
at
at

0.11 "$ns trace-annotate \"Send Packet_0\""


0.35 "$ns trace-annotate \"Receive Ack_0\""
0.56 "$ns trace-annotate \"Send Packet_1\""
0.79 "$ns trace-annotate \"Receive Ack_1\""
0.99 "$ns trace-annotate \"Send Packet_2\""
1.23 "$ns trace-annotate \"Receive Ack_2 \""
1.43 "$ns trace-annotate \"Send Packet_3\""
1.67 "$ns trace-annotate \"Receive Ack_3\""
1.88 "$ns trace-annotate \"Send Packet_4\""
2.11 "$ns trace-annotate \"Receive Ack_4\""
2.32 "$ns trace-annotate \"Send Packet_5\""
2.55 "$ns trace-annotate \"Receive Ack_5 \""
2.75 "$ns trace-annotate \"Send Packet_6\""
2.99 "$ns trace-annotate \"Receive Ack_6\""
3.1 "$ns trace-annotate \"FTP stops\""

$ns run

Screenshots Of the Output:


Before 0.1 ms:

After 0.1 ms:

After 0.5 ms:

After 0.8 ms:

Selective Repeat

Program:
#include<iostream>
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#define cls() printf(33[H33[J)
struct frame
//structure definition for designing the packet.
{
int packet[40];
};
struct ack
//structure definition for accepting the
acknowledgement.
{
int acknowledge[40];
};
int main()
{
int serversocket;
sockaddr_in serveraddr,clientaddr;
socklen_t len;
int
windowsize,totalpackets,totalframes,framessend=0,i=0,j=0,k,l,m,n,repacket[4
0];
ack acknowledgement;
frame f1;
char req[50];
serversocket=socket(AF_INET,SOCK_DGRAM,0);
bzero((char*)&serveraddr,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(5018);
serveraddr.sin_addr.s_addr=INADDR_ANY;
bind(serversocket,(sockaddr*)&serveraddr,sizeof(serveraddr));
bzero((char*)&clientaddr,sizeof(clientaddr));
len=sizeof(clientaddr);
//connection establishment.
printf(\nWaiting for client connection.\n);
recvfrom(serversocket,req,sizeof(req),0,(sockaddr*)&clientaddr,&len);
printf(\nThe client connection obtained.\t%s\n,req);
//sending request for windowsize.
printf(\nSending request for window size.\n);
sendto(serversocket,REQUEST FOR WINDOWSIZE.,sizeof(REQUEST FOR
WINDOWSIZE.),0,(sockaddr*)&clientaddr,sizeof(clientaddr));
//obtaining windowsize.
printf(\nWaiting for the windowsize.\n);
recvfrom(serversocket,(char*)&windowsize,sizeof(windowsize),0,
(sockaddr*)&clientaddr,&len);
cls();
printf(\nThe windowsize obtained as:\t%d\n,windowsize);
printf(\nObtaining packets from network layer.\n);
printf(\nTotal packets obtained:\t%d\n,(totalpackets=windowsize*5));
printf(\nTotal frames or windows to be transmitted:\t%d\n,(totalframes=5));
//sending details to client.
printf(\nSending total number of packets.\n);
sendto(serversocket,(char*)&totalpackets,sizeof(totalpackets),0,
(sockaddr*)&clientaddr,sizeof(clientaddr));

recvfrom(serversocket,req,sizeof(req),0,(sockaddr*)&clientaddr,&len);
printf(\nSending total number of frames.\n);
sendto(serversocket,(char*)&totalframes,sizeof(totalframes),0,
(sockaddr*)&clientaddr,sizeof(clientaddr));
recvfrom(serversocket,req,sizeof(req),0,(sockaddr*)&clientaddr,&len);
printf(\nPRESS ENTER TO START THE PROCESS.\n);
fgets(req,2,stdin);
cls();
j=0;
l=0;
//starting the process of sending
while( l<totalpackets)
{
//initialising the transmit buffer.
bzero((char*)&f1,sizeof(f1));
printf(\nInitialising the transmit buffer.\n);
printf(\nThe frame to be send is %d with packets:\t,framessend);
//Builting the frame.
for(m=0;m<j;m++)
{
//including the packets for which negative acknowledgement
was received.
printf(%d ,repacket[m]);
f1.packet[m]=repacket[m];
}
while(j<windowsize && i<totalpackets)
{
printf(%d ,i);
f1.packet[j]=i;
i++;
j++;
}
printf(\nSending frame %d\n,framessend);
//sending the
frame.
sendto(serversocket,(char*)&f1,sizeof(f1),0,
(sockaddr*)&clientaddr,sizeof(clientaddr));
//Waiting for the acknowledgement.
printf(\nWaiting for the acknowledgement.\n);
recvfrom(serversocket,
(char*)&acknowledgement,sizeof(acknowledgement),0,
(sockaddr*)&clientaddr,&len);
cls();
//Checking acknowledgement of each packet.
j=0;
k=0;
m=0;
n=l;
while(m<windowsize && n<totalpackets)
{
if(acknowledgement.acknowledge[m]==-1)
{
printf(\nNegative acknowledgement received for packet:
%d\n,f1.packet[m]);
k=1;
repacket[j]=f1.packet[m];
j++;
}
else

{
l++;
}
m++;
n++;
}
if(k==0)
{
printf(\nPositive acknowledgement received for all packets within the frame:
%d\n,framessend);
}
framessend++;
printf(\nPRESS ENTER TO PROCEED\n);
fgets(req,2,stdin);
cls();
}
printf(\nAll frames send successfully.\n\nClosing connection with the
client.\n);
close(serversocket);
}
Output:

Client Server
Program

import java.io.*;
import java.net.*;
class client
{
public static void main(String arg[])
{
int port=9999;
Socket s;
String msg="";
try
{
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
InetAddressaddr=InetAddress.getLocalHost();
s=new Socket(addr,port);
OutputStreamWriterosw=new OutputStreamWriter(s.getOutputStream());
PrintWriter pw=new PrintWriter(osw);
BufferedReader br1=new BufferedReader(new
InputStreamReader(s.getInputStream()));
System.out.print("Enter a Number : ");
String str=br.readLine();
pw.println(str);
pw.flush();
msg=br1.readLine();
System.out.println("Answer from server : ");
System.out.println(msg);
}
catch(Exception e)
{
// Ignore
}
}
}
import java.io.*;
import java.net.*;
class factserver implements Runnable
{
Socket s;
int id;
public static void main(String arg[])
{
int port=9999,count=0;
try
{
ServerSocketss=new ServerSocket(port);
System.out.println("Waiting for client");
while(true)
{

Socket s=ss.accept();
factserver serve=new factserver(s,count);
Thread t=new Thread(serve);
t.start();
}
}
catch(Exception e)
{
System.out.println("Error");
}
}
factserver(Socket s,int id)
{
this.s = s;
this.id = id;
}
public void run()
{
try
{
BufferedReaderbr=new BufferedReader(new
InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(new
OutputStreamWriter(s.getOutputStream())) ;
String str=br.readLine();
int n=Integer.parseInt(str);
inti;
long f=1;
System.out.println("Number sent by client: " + n);
for(i = 2; i<= n; i++)
f = f * i;
pw.println("Factorial is : "+f);
pw.flush();
}
catch(Exception e)
{
System.out.println("Thread: Error");
}
}
}

OUTPUT

Socket Programming

Program:
#include<iostream>
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
int main()
{
int serversocket,clientsocket;

//inculsion.

//variables to store the socket id.


//variables to store the network

host addresses.
sockaddr_in serveraddr,clientaddr;

//variable to store address

length.

socklen_t len;
char message[50];
//creating a socket.
serversocket=socket(AF_INET,SOCK_STREAM,0); //steps to include the host

address

bzero((char*)&serveraddr,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(5030);
serveraddr.sin_addr.s_addr=INADDR_ANY;
//binding the socket to the operating

system.
bind(serversocket,(sockaddr*)&serveraddr,sizeof(serveraddr));
bzero((char*)&clientaddr,sizeof(clientaddr));
len=sizeof(clientaddr);
//listening over the socket.
listen(serversocket,5);
printf(\nWaiting for client connectivity.\n);
//accepting the connection.
clientsocket=accept(serversocket,(sockaddr*)&clientaddr,&len);
printf(\nClient connectivity received.\n);
printf(\nReading message from the client.\n);

//reading

activity.
read(clientsocket,message,sizeof(message));
printf(\nThe client has send:\t%s\n,message);
printf(\nSending message to the client.\n);

//writing

activity.
write(clientsocket,YOUR MESSAGE RECEIVED.,sizeof(YOUR MESSAGE
RECEIVED.));
close(clientsocket);
close(serversocket);
}

Output:

NS2

Wireshark:

You might also like