Showing posts with label IMPLEMENTATION OF SLIDING WINDOW PROTOCOL USING SOCKETS. Show all posts
Showing posts with label IMPLEMENTATION OF SLIDING WINDOW PROTOCOL USING SOCKETS. Show all posts

SLIDING WINDOW PROTOCOL USING SOCKETS

SERVER PROGRAM

#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

int main()
{
        int sock,size,connect;
        char senddata[50],data[50];
        int val,count,i,port;
        struct sockaddr_in ser,cli;
        printf("\n\n Server Running ...... ");
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
           perror("\n Socket Creation Error");
            exit(-1);
        }
        printf("\nEnter the port number  : ");
        scanf("%d",&port);
        ser.sin_family = AF_INET;
        ser.sin_port = htons(port);
        ser.sin_addr.s_addr=INADDR_ANY;
        bzero(&(ser.sin_zero),8);
if(bind(sock,(struct sockaddr *)&ser,sizeof(struct sockaddr)) == -1)
        {
            perror("\n\t Error in Bind");
            exit(-1);
        }
if (listen(sock,2)==-1)
        {
             perror("\n\t Error in Listen");
             exit(-1);
        }
printf("\n\t Waiting for connection ");
   size=sizeof(struct sockaddr);
   connect=accept(sock,(struct sockaddr *)&cli,&size);
if(connect==-1)
    {
      perror("\n\t Connection Failed :");
      exit(-1);
    }
    printf("\n\t Connected Successfully");
    printf("\n");
        // get the pocket number from client
    recv(connect,&val,sizeof(val),0);
    count=val;
      while(1)
      {
            i=recv(connect,&data,sizeof(data),0);
            data[i]='\0';
            if (strcmp(data,"end")==0)
           {
               printf("\n\t Finished");
               break;
           }
           if(count!=val)
           {
            strcpy(senddata,"packet missing");
                send(connect,&count,sizeof(count),0);
                send(connect,senddata,strlen(senddata),0);
            }
          else
          {
             printf("\n The packet Number is : %d",val);
             printf("\n The data is :%s",data);
             count++;
             strcpy(senddata,"send nextdata");
             send(connect,&count,sizeof(count),0);
             send(connect,senddata,strlen(senddata),0);
          }

        printf("\n The Expected Packet now is: %d \n",count);
        recv(connect,&val,sizeof(val),0);
     }
close(connect);
close(sock);
return 0;
}
CLIENT PROGRAM

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

int main()
{
        int sock,val,i,count,port;
        char recvdata[50],sentdata[50];
        struct sockaddr_in server_addr;
        printf("\n\n Client Running ...... ");
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
            perror("Socket");
            exit(1);
        }
        printf("\nEnter the port number");
        scanf("%d",&port);
        server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(port);
        server_addr.sin_addr.s_addr= htonl(INADDR_ANY);
        bzero(&(server_addr.sin_zero),8);
        if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockadd
r)) == -1)
        {
            perror("Connect");
            exit(1);
        }
        while(1)
        {
                //get the pack number from client
                printf("\n Enter packet number ");
                scanf("%d",&val);
                // sent the value to server
                send(sock,&val,sizeof(val),0);
                // get the data from the user
                printf("\n\n Enter data ");
                scanf("%s",sentdata);
                // sent the to server
                send(sock,sentdata,strlen(sentdata),0);
                   if(strcmp(sentdata,"end")==0)
                   break;
                // recev the result from server
                recv(sock,&count,sizeof(count),0);
                i=recv(sock,recvdata,50,0);
                recvdata[i]='\0';
                printf("\n %s %d",recvdata,count);
        }
        close(sock);
        return 0;
}

OUTPUT
SERVER

[11ca013@mcalinux network]$ cc 17slideser.c
[11ca013@mcalinux network]$  ./a.out


 Server Running ......
Enter the port number  : 3326

         Waiting for connection
         Connected Successfully

 The packet Number is : 18
 The data is :google
 The Expected Packet now is: 19

 The packet Number is : 19
 The data is :yahoo
 The Expected Packet now is: 20

 The Expected Packet now is: 20

 The packet Number is : 20
 The data is :ok
 The Expected Packet now is: 21






CLIENT

[11ca013@mcalinux network]$ cc 17slidecli.c
[11ca013@mcalinux network]$ ./a.out

 Client Running ......
Enter the port number3326
 Enter packet number 18


 Enter data google

 send nextdata 19
 Enter packet number 19


 Enter data yahoo

 send nextdata 20
 Enter packet number 26

 Enter data hi

 packet missing 20
 Enter packet number 20


 Enter data ok



Your IP Address is:

Browser: