Advertisement
Bewin

Selective Repeat Client

Mar 24th, 2025
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | None | 0 0
  1. #include <netdb.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/socket.h>
  6. #include <time.h>
  7. #include <sys/time.h>
  8. #include <unistd.h>
  9. #include <arpa/inet.h>
  10. #define MAX 80
  11. #define PORT 8080
  12. #define SA struct sockaddr
  13. struct timeval timeout;
  14. void func(int sockfd, int nf, int ws)
  15. {
  16.     char buff[MAX];
  17.     int ack, i = 0, n, k, w1 = 0, w2 = ws - 1, j, flag = 0, count_ack = 0;
  18.     if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout)) < 0)
  19.         perror("setsockopt(SO_RCVTIMEO) failed");
  20.     for (i = 0; i < nf && i <= w2; i++)
  21.     {
  22.         bzero(buff, sizeof(buff));
  23.         snprintf(buff, sizeof(buff), "%d", i);
  24.         k = send(sockfd, buff, sizeof(buff), 0);
  25.         printf("Frame %d sent\n", i);
  26.     }
  27.     while (1)
  28.     {
  29.         if (count_ack == nf)
  30.         {
  31.             strcpy(buff, "Exit");
  32.             k = send(sockfd, buff, sizeof(buff), 0);
  33.             break;
  34.         }
  35.         if (w2 - w1 != ws - 1 && flag == 0 && i != nf)
  36.         {
  37.             bzero(buff, sizeof(buff));
  38.             snprintf(buff, sizeof(buff), "%d", i);
  39.             k = send(sockfd, buff, sizeof(buff), 0);
  40.             printf("Frame %d sent\n", i);
  41.             w2++;
  42.             i++;
  43.         }
  44.         flag = 0;
  45.         bzero(buff, sizeof(buff));
  46.         n = recv(sockfd, buff, MAX, 0);
  47.         ack = atoi(buff);
  48.         if (n > 0)
  49.         {
  50.             if (ack + 1 == nf)
  51.             {
  52.                 printf("Acknowledgement received: %d\n", ack);
  53.                 count_ack++;
  54.                 bzero(buff, sizeof(buff));
  55.                 /*strcpy(buff, "Exit");
  56.                 k = send(sockfd, buff, sizeof(buff), 0);
  57.                 break;*/
  58.             }
  59.             else if (ack == -1)
  60.             {
  61.                 printf("Acknowledgement not received for %d\nResending frame\n", w1);
  62.                 bzero(buff, sizeof(buff));
  63.                 snprintf(buff, sizeof(buff), "%d", w1);
  64.                 k = send(sockfd, buff, sizeof(buff), 0);
  65.                 printf("Frame sent: %d\n", w1);
  66.             }
  67.             else
  68.             {
  69.                 w1++;
  70.                 printf("Acknowledgement received: %d\n", ack);
  71.                 count_ack++;
  72.             }
  73.         }
  74.     }
  75. }
  76. void main()
  77. {
  78.     int sockfd, connfd, f, w;
  79.     struct sockaddr_in servaddr, cli;
  80.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  81.     if (sockfd == -1)
  82.     {
  83.         printf("Socket creation failed\n");
  84.         exit(0);
  85.     }
  86.     else
  87.         printf("Socket successfully created\n");
  88.     bzero(&servaddr, sizeof(servaddr));
  89.     servaddr.sin_family = AF_INET;
  90.     servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  91.     servaddr.sin_port = htons(PORT);
  92.     timeout.tv_sec = 3;
  93.     timeout.tv_usec = 0;
  94.     if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) != 0)
  95.     {
  96.         printf("Connection with the server failed\n");
  97.         exit(0);
  98.     }
  99.     else
  100.         printf("Connected to the server\n");
  101.     printf("Enter the number of frames: ");
  102.     scanf("%d", &f);
  103.     printf("Enter the window size: ");
  104.     scanf("%d", &w);
  105.     func(sockfd, f, w);
  106.     close(sockfd);
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement