Bewin

Go-Back-N-ARQ Client

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