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

10 Response to " SLIDING WINDOW PROTOCOL USING SOCKETS"

  1. Unknown says:

    thanks...!!

    Unknown says:

    thank you....

    squares says:

    Nice Information,
    Thanks For Sharing..
    upvc doors and windows manufacturers
    upvc doors manufacturers in hyderabad

    Unknown says:

    1234567890-==================['/.,mnbvcxxzASDFGHJKL;'

    We square measure a quality-committed company that helps us earned the name as top-notch Manufacturer and Supplier of Modular Kitchen and Kitchen Cabinet. Browse our range and if you've got any personalized demand, be happy to share with us. We’ll be happy to serve you the merchandise, you’ve been checking out.
    Modular Kitchen
    Kitchen Cabinet in Hyderabad
    Manufacturer and Supplier of Mosquito Mesh Window
    Manufacturer and Supplier of Sliding Window in Hyderabad

    Unknown says:

    Sir can u send these program in java

    Unknown says:

    Thankyou so much for the quality content, it is unique and very easily understandable. Keep updating the information.

    https://www.sudhakarind.com/windows-doors/

    Thank you for valuable and informatic content please keep go on .
    https://www.sudhakarind.com/windows-doors/windows-slider-system/

    Unknown says:

    Thankyou so much for the quality content, it is unique and very easily understandable.
    https://www.sudhakarind.com/?s=Windows+and+Doors

    Good post! We are linking to this particularly great post on our websites.
    educatorpage
    Bloglovin
    gumroad
    toparticle
    door site
    Zenwriting

Post a Comment



Your IP Address is:

Browser: