Advertisement
4doorsmorehories

Stop and Wait Server

Apr 18th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include <netinet/in.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/socket.h>
  6. #include <unistd.h>
  7. #define PORT 8080
  8. #define MAX 80
  9. void transfer(int sock){
  10. struct timeval tv;
  11. tv.tv_sec = 1;
  12. tv.tv_usec = 0;
  13. setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
  14.       printf("Send\tFrame status\tACK status");
  15.         int *n,i=0,j,o,k=0;
  16.         n=&i;
  17.        for(j=0;j<10;j++){
  18.          o=i;
  19.         TES:
  20.         k++;
  21.           i=j%2;
  22.          
  23.  
  24.           write(sock,n,sizeof(i));
  25.           printf("\n%d\t Frame no:%d\t",j,i);
  26.           if(k==3){
  27.           printf("ACK missing");
  28.           goto TES;
  29.           continue;
  30.           }
  31.           if(read(sock,n,sizeof(i))){
  32.           if(i==o){
  33.             printf("Frame missing");
  34.             goto TES;
  35.           }
  36.           else
  37.              printf("ACK no:%d",i);
  38.              
  39.           }
  40.          
  41.          
  42.       }
  43.        
  44. }
  45.  
  46. int main(int argc, char const* argv[])
  47. {
  48.     int server_fd, new_socket;
  49.     struct sockaddr_in address;
  50.     int opt = 1,n;
  51.     int addrlen = sizeof(address);
  52.  
  53.    
  54.  
  55.     // Creating socket file descriptor
  56.     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))
  57.         == 0) {
  58.         perror("socket failed");
  59.         exit(EXIT_FAILURE);
  60.     }
  61.  
  62.     // Forcefully attaching socket to the port 8080
  63.     if (setsockopt(server_fd, SOL_SOCKET,
  64.                 SO_REUSEADDR | SO_REUSEPORT, &opt,
  65.                 sizeof(opt))) {
  66.         perror("setsockopt");
  67.         exit(EXIT_FAILURE);
  68.     }
  69.     address.sin_family = AF_INET;
  70.     address.sin_addr.s_addr = INADDR_ANY;
  71.     address.sin_port = htons(PORT);
  72.  
  73.     // Forcefully attaching socket to the port 8080
  74.     if (bind(server_fd, (struct sockaddr*)&address,
  75.             sizeof(address))
  76.         < 0) {
  77.         perror("bind failed");
  78.         exit(EXIT_FAILURE);
  79.     }
  80.     if (listen(server_fd, 3) < 0) {
  81.         perror("listen");
  82.         exit(EXIT_FAILURE);
  83.     }
  84.     if ((new_socket
  85.         = accept(server_fd, (struct sockaddr*)&address,
  86.                 (socklen_t*)&addrlen))
  87.         < 0) {
  88.         perror("accept");
  89.         exit(EXIT_FAILURE);
  90.     }
  91.    
  92.     transfer(new_socket);
  93. // closing the connected socket
  94.     close(new_socket);
  95. // closing the listening socket
  96.     shutdown(server_fd, SHUT_RDWR);
  97.     return 0;
  98. }
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement