livesinabox 0 Newbie Poster

What I am supposed to do is create a C program which implements a "dumb" Sliding Window Protocol Implementing Client Server Program.

First the client and server establish a connection between themselves and then the client sends "RST" to the server. The server inturn sends its window size (10) to the client. The client sends its data packets(basically containing sequence numbers) in batches of 10. After sending each batch it sends an "END" packet. When the server recieves an "END" packet it must inturn send "ACK" ( next expected packet number).

My client seems to work fine. But my server basically stops working after sending the window size. It doesn't print recieved data or calculate the ACK.

Note: I am assuming my connection establishment part is right. I excute it in my Linux Terminal like so:
Server: ./a.out 5500
Client: ./a.out localhost 5500

Here is my code:

//SERVER:

#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<string.h>
#include<sys/types.h>

int main(int argc, char **argv)
{

int listenfd,connfd,len,i,packets,window,flag,nextpack;
struct sockaddr_in servaddr,caddr;

char rbuf[100],req[100],ack[100];
window=10;

 if((listenfd= socket(AF_INET,SOCK_STREAM,0))<0)
   printf("Socket Creation Error\n");

 bzero((struct sockaddr*)&servaddr,sizeof(servaddr));
 servaddr.sin_family=AF_INET;
 servaddr.sin_port=htons(atoi(argv[1]));
 servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

 if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
   fprintf(stderr,"Bind Error\n");
   listen(listenfd,15);
//iterative server
for(;;)
{
   len=sizeof(caddr); 
   connfd=accept(listenfd,(struct sockaddr*)&caddr,&len);
   if(connfd>0)
    fprintf(stderr,"Connection Successful! connfd=%d",connfd);
  //sleep(1);
   read(connfd,rbuf,3);//get request (RST)
   fprintf(stderr,"\n %s",rbuf);
   write(connfd,"10",2);//send winsize

for(;;)
{
 read(connfd,rbuf,5);//get data from client
 fprintf(stderr,rbuf,5);

 if(strcmp(rbuf,"END")!=0)// if the data recieved is not end then increment packet counter
  packets+=1;
 else //if the data received is END then ACK <next expected packet number> is to be sent
 {
  nextpack=packets+1;
  sprintf(ack,"%d",nextpack);
  write(connfd,ack,5);
 }
}
 close(connfd);
}//for
}//main


//CLIENT:

#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<string.h>

int main(int argc, char **argv)
{
 int sockfd,window,packets,limit,flag,i,choice,data,ack;
 struct sockaddr_in servaddr,cliaddr;
 char rbuf[300]="",winstring[100],req[100],ackstr[100];

data =0;

 if((sockfd= socket(AF_INET,SOCK_STREAM,0))<0)
   printf("socket creation error");
  else
   printf("Socket Created Successfully\n");

 bzero((struct sockaddr*)&servaddr,sizeof(servaddr));
 servaddr.sin_family=AF_INET;
 servaddr.sin_port=htons(atoi(argv[2]));
 inet_aton(argv[1],&servaddr.sin_addr);


 if(connect(sockfd,(struct sockaddr*) &servaddr,sizeof(servaddr))<0)
  printf("connection error ");

write(sockfd,"RST",3); //send RST
read(sockfd,winstring,2);  //get window size
window=atoi(winstring);

do{
printf("\nEnter Number of packets to send\n");
scanf("%d",&packets);
while(packets>0)
{   
 if(packets > window) // splitting up packets so that data is sent in batches of 10(window size)
   limit=window;
 else
  limit=packets;

 for(i=0;i<limit;i++)
 { fprintf(stdout,"Sending Data : %d\n",data); 
   write(sockfd,data,100); //sending data
   data+=1;
 }

write(sockfd,"END",200); // sending END after batch is sent
read(sockfd,ackstr,5);//read ack
printf("ACK Received : %s",ackstr); //print ACK number
packets-=window;
}//while
printf("Do you want to send more packets? 1 for Yes.");
scanf("%d",&choice);
}while(choice==1);
close(sockfd);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.