title: Implementing Stop and wait with C & UDP, and what does cause 'resource temporarily unavailable'?
body: <p>I am implementing stop&amp;wait with c and udp socket programming. To simulate this protocol, I wrote two codes.</p>
  
  <p>This is my <code>server.c</code> file:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  
  typedef struct packet{
      char data[1024];
  }Packet;
  
  typedef struct frame{
      int frame_kind; //ACK:0, SEQ:1 FIN:2
      int sq_no;
      int ack;
      Packet packet;
  }Frame;
  
  int main(int argc, char** argv)
  {
      int sockfd;
      int clilen;
      int state;
      int n;
      int sum;
      int recv_result;
  
      int frame_id = 0;
  
      Packet packet;
      Frame frame;
      Frame recv_frame;
      char buffer[1024] = "";
  
      struct timeval tv;
  
      struct sockaddr_in serveraddr, clientaddr;
  
      FILE* infile = fopen(argv[2], "r");
      if(argc != 3)
      {
          perror("error! usage: $./server &lt;PORT&gt; filename\n");
          exit(1);
      }
      if(infile == NULL)
      {
          printf("error! failed to open the file.\n");
          exit(0);
      }
  
      clilen = sizeof(clientaddr);
      sockfd = socket(AF_INET, SOCK_DGRAM, 0); //using diagram instead of stream-- UDP
      if(sockfd &lt; 0)
      {
          perror("socket error: ");
          exit(0);
      }
  
      bzero(&amp;serveraddr, sizeof(serveraddr));
      serveraddr.sin_family = AF_INET;
      serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
      serveraddr.sin_port = htons(atoi(argv[1])); //set port number 9999
  
      //set socket option -- timeout is 100000 microseconds
  
      tv.tv_sec = 1;
      tv.tv_usec = 0;
      setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char*)&amp;tv, sizeof(struct timeval));
  
      state = bind(sockfd, (struct sockaddr*)&amp;serveraddr, sizeof(serveraddr));
      if(state == -1)
      {
          perror("bind error: ");
          exit(0);
      }
  
      do{
          n = fscanf(infile, "%s", buffer);
  
          strcpy(packet.data, buffer);
          memcpy(&amp;(frame.packet), &amp;packet, sizeof(Packet));
          frame.frame_kind = 1; //SEQ
          frame.sq_no = frame_id;
          frame.ack = 0;
          printf("flag2");
          while(1)
          {
              sendto(sockfd, &amp;frame, sizeof(frame), 0, (struct sockaddr*)&amp;clientaddr, (socklen_t)clilen);
              printf("Frame %d sent\n", frame_id);
              recv_result = recvfrom(sockfd, &amp;recv_frame, sizeof(recv_frame), 0,(struct sockaddr*)&amp;clientaddr, &amp;clilen );
  
              if(recv_result &gt; 0 &amp;&amp; recv_frame.sq_no == 0 &amp;&amp; recv_frame.ack == frame_id){
                  printf("Ack %d received\n", recv_frame.sq_no);
                  break;
              }else{
                  printf("Frame %d time expired\n", frame_id);
              }
          }
          frame_id++;
      }while(n &gt; 0);
  
      printf("finished\n");
      return 0;
  }
  </code></pre>
  
  <p>This is my <code>client.c</code> file:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;sys/socket.h&gt;
  
  typedef struct packet{
      char data[1024];
  }Packet;
  
  typedef struct frame{
      int frame_kind; //ACK:0, SEQ:1 FIN:2
      int sq_no;
      int ack;
      Packet packet;
  }Frame;
  
  int main(int argc, char *argv[])
  {
      int sock;
      int str_len;
      socklen_t adr_sz;
      FILE* outfile = fopen("output.txt", "w");
      struct sockaddr_in serv_adr, from_adr;
      Frame ackframe, recv_frame;
      int frame_id;
      int recv_result;
      struct timeval tv;
  
      if(argc!=3){
          printf("Usage : %s &lt;IP&gt; &lt;port&gt;\n", argv[0]);
          exit(1);
      }
      sock=socket(PF_INET, SOCK_DGRAM, 0);   
      if(sock==-1)
      {
          printf("error! failed to open the socket\n");
          exit(1);
      }
  
      memset(&amp;serv_adr, 0, sizeof(serv_adr));
      adr_sz = sizeof(serv_adr);
      serv_adr.sin_family=AF_INET;
      serv_adr.sin_addr.s_addr=inet_addr(argv[1]);
      serv_adr.sin_port=htons(atoi(argv[2]));
  
      tv.tv_sec = 1;
      tv.tv_usec = 0;
      setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&amp;tv, sizeof(struct timeval));
  
      frame_id = 0;
      while(1)
      {
          recv_result = recvfrom(sock, &amp;recv_frame, sizeof(recv_frame), 0,(struct sockaddr*)&amp;serv_adr, &amp;adr_sz);
          perror("error: ");
          printf("data %s\n", recv_frame.packet.data);
          printf("recv result %d\n", recv_result);
          printf("recv frame=%d, frame_kind = %d\n", recv_frame.sq_no, recv_frame.frame_kind);
          if(recv_result &gt; 0 &amp;&amp; recv_frame.frame_kind == 1 &amp;&amp; recv_frame.sq_no==frame_id){
              printf("Frame %d received\n", recv_frame.sq_no);
              fprintf(outfile, "%s", recv_frame.packet.data);
              ackframe.frame_kind = 0;
              ackframe.ack = recv_frame.sq_no + 1;
              printf("Ack %d sent\n", ackframe.ack);
              sendto(sock, &amp;recv_frame, sizeof(recv_frame), 0,(struct sockaddr*)&amp;serv_adr, adr_sz);
  
  
              break;
          }else{
              printf("Frame %d time expired\n", frame_id);
          }
      }   
      close(sock);
      fclose(outfile);
      return 0;
  }
  </code></pre>
  
  <p>The server needs 3 arguments: <code>./server &lt;port&gt; &lt;test.txt&gt;</code></p>
  
  <p>The client needs also 3 arguments: <code>./client &lt;ip&gt; &lt;port&gt;</code></p>
  
  <p>It is executed, but <code>recvfrom</code> of client always returns <code>-1</code>.</p>
  
  <p>I read some questions with same topic, but could not find out the error from my code. Is there any way to fix my code with <code>recvfrom</code> &amp; <code>sendto</code>? Or do I need to use <code>select</code> function? (I will only communicate with one server &amp; one client.)</p>