Você está na página 1de 10

Computer Networks Assignment

Implementation of Go back N ARQ Protocol

ByAashwin Jain 09bce262 Akash Sharma 09bce394

Table of Contents

Introduction Procedure Pseudocode Implementation in C References

Introduction
A network protocol defines rules and conventions for communication between network devices. Protocols for computer networking all generally use packet switching techniques to send and receive messages in the form of packets. Network protocols include mechanisms for devices to identify and make connections with each other, as well as formatting rules that specify how data is packaged into messages sent and received. Some protocols also support message acknowledgement and data compression designed for reliable and/or high-performance network communication. Hundreds of different computer network protocols have been developed each designed for specific purposes and environments. Modern operating systems like Microsoft Windows contain built-in services or daemons that implement support for some network protocols. Applications like Web browsers contain software libraries that support the high level protocols necessary for that application to function. For some lower level TCP/IP and routing protocols, support is implemented in directly hardware (silicon chipsets) for improved performance.

Procedure
Go-Back-N ARQ is a specific instance of the automatic repeat request (ARQ) protocol, in which the sending process continues to send a number of frames specified by a window size even without receiving an acknowledgement (ACK) packet from the receiver. It is a special case of the general sliding window protocol with the transmit window size of N and receive window size of 1. In sliding window protocols the sender's data link layer maintains a 'sending window' which consists of a set of sequence numbers corresponding to the frames it is permitted to send. Similarly, the receiver maintains a 'receiving window' corresponding to the set of frames it is permitted to accept. The window size is dependent on the retransmission policy and it may differ in values for the receiver's and the sender's window. The sequence numbers within the sender's window represent the frames sent but as yet not acknowledged. Whenever a new packet arrives from the network layer, the upper edge of the window is advanced by one. When an acknowledgement arrives from the receiver the lower edge is advanced by one. The receiver's window corresponds to the frames that the receiver's data link layer may accept. When a frame with sequence number equal to the lower edge of the window is received, it is passed to the network layer, an acknowledgement is generated and the window is rotated by one. If however, a frame falling outside the window is received, the receiver's data link layer has two options. It may either discard this frame and all subsequent frames until the desired frame is received or it may accept these frames or buffer them until the appropriate frame is received and then pass the frames to the network layer in sequence.

Pseudocode
This example assumes an infinite number of sequence and request numbers. Rn = request number Sn = sequence number Sb = sequence base Sm = sequence max Receiver: Rn = 0 Do the following forever: If the packet received = Rn && the packet is error free Accept the packet and send it to a higher layer Rn = Rn + 1 Send a Request for Rn Else Refuse packet Send a Request for Rn Sender: Sb = 0 Sm = N 1 Repeat the following steps forever: 1. If you receive a request number where Rn > Sb Sm = Sm + (Rn Sb) Sb = Rn 2. If no packet is in transmission, Transmit a packet where Sb <= Sn <= Sm. Packets are transmitted in order.

Implementation in C
Sender side
#include<stdio.h> #include<sys/types.h> #include<netinet/in.h> #include<string.h> #include<sys/socket.h> #include<stdlib.h> #include<unistd.h> main() { int sd,i,len,bi,nsd,port,j,k,z,x; char content[]={"my name is aashwinl"}; char buff[5],buffn[5]; struct sockaddr_in ser,cli; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("\nSocket problem"); return 0; } printf("\nSocket created\n"); bzero((char*)&cli,sizeof(ser)); printf("ENTER PORT NO:\n"); scanf("%d",&port); printf("\nPort Address is %d\n:",port); ser.sin_family=AF_INET; ser.sin_port=htons(port); ser.sin_addr.s_addr=htonl(INADDR_ANY); bi=bind(sd,(struct sockaddr *)&ser,sizeof(ser)); if(bi==-1) { printf("\nBind error, Port busy, Plz change port in client and server"); return 0;

} i=sizeof(cli); listen(sd,5); nsd = accept(sd,((struct sockaddr *)&cli),&i); if(nsd==-1) { printf("\nCheck the description parameter\n"); return 0; } printf("\nConnection accepted!\n"); j=0; mad: k=0; while(content[j]!='\0' && k<5) { buff[k]=content[j]; j++; k++; } buff[k]='\0'; again: printf("sending %s",buff); x=rand()%4; if(x==0 ) goto middle; send(nsd,buff,5,0); printf("\nwaiting for ack\n"); sleep(2); recv(nsd,buffn,2,0); x=buffn[0]; x=x-48; j=j-5+x; printf("\n%d\n",x+1); printf("\nRecevied ack\n"); if(x==6)

goto end; goto mad; middle: printf("\nwaiting for ack\n"); sleep(2); printf("\nno ack sending again\n"); goto again; end: printf("\nBye"); close(sd); close(nsd); return 0; }

Receiver side
int main() { int sd,con,port,i,j,Res,x; char content[30],buff[2],content2[30]; struct sockaddr_in cli; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("\nSocket problem"); return 0; } bzero((char*)&cli,sizeof(cli)); cli.sin_family = AF_INET; printf("ENTER PORT NO:\n"); scanf("%d",&port); cli.sin_port=htons(port); cli.sin_addr.s_addr=htonl(INADDR_ANY); con=connect(sd,(struct sockaddr*)&cli,sizeof(cli)); if(con==-1) { printf("\nConnection error");

return 0; } i=0; j=0; mad: recv(sd,content,5,0); if(content[0]=='l' || content [1]=='l' || content[2]=='l' || content[3]=='l' || content[4]=='l') goto end; x=rand()%4; content[x]='?'; printf("\nServer: "); for(i=0;i<x;i++) printf("%c",content[i]); x=x+48; buff[0]=x; buff[1]='\0'; send(sd,buff,2,0);

goto mad; end: printf("\nServer: %s\nData transfer complete\n",content); buff[0]=54; buff[1]='\0'; send(sd,buff,2,0); close(sd); return 0; }

References
Kurose, James F.; Keith W. Ross. Computer Networking: A Top-Down Approach Behrouz Forouzan fifth edition Data communication and computer networks

Você também pode gostar