title: file transfer using stop and wait protocol and udp protocol in C
body: <p>I want to send a file using UDP with stop and wait protocol. I have a client and server code that is working fine with a single input string.</p>
  <p>I was trying to change the code for file transfer but no success. Can anyone help me with minimal changes to convert the below code for using file transfer?</p>
  <p>code of client.c</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[]){
      if (argc != 2){
          printf(&quot;Usage: %s &lt;port&gt;&quot;, argv[0]);
          exit(0);    
      }
  
  int port = atoi(argv[1]);
  int sockfd;
  struct sockaddr_in serverAddr;
  char buffer[1024];
  socklen_t addr_size;
  
  int frame_id = 0;
  Frame frame_send;
  Frame frame_recv;
  int ack_recv = 1;
  
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  
  memset(&amp;serverAddr, '\0', sizeof(serverAddr));
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(port);
  serverAddr.sin_addr.s_addr = inet_addr(&quot;127.0.0.1&quot;);
  
  while(1){
      
      if(ack_recv == 1){
          frame_send.sq_no = frame_id;
          frame_send.frame_kind = 1;
          frame_send.ack = 0;     
  
          printf(&quot;Enter Data: &quot;);
          scanf(&quot;%s&quot;, buffer);
          strcpy(frame_send.packet.data, buffer);
          sendto(sockfd, &amp;frame_send, sizeof(Frame), 0, (struct sockaddr*)&amp;serverAddr, sizeof(serverAddr));
          printf(&quot;[+]Frame Send\n&quot;);
      }
      int addr_size = sizeof(serverAddr);
      int f_recv_size = recvfrom(sockfd, &amp;frame_recv, sizeof(frame_recv), 0 ,(struct sockaddr*)&amp;serverAddr, &amp;addr_size);
      
      if( f_recv_size &gt; 0 &amp;&amp; frame_recv.sq_no == 0 &amp;&amp; frame_recv.ack == frame_id+1){
          printf(&quot;[+]Ack Received\n&quot;);
          ack_recv = 1;
      }else{
          printf(&quot;[-]Ack Not Received\n&quot;);
          ack_recv = 0;
      }   
          
      frame_id++;     
  }
  
  close(sockfd);
  return 0;
  }
  </code></pre>
  <p>Code of server.c</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){
  
      if (argc != 2){
          printf(&quot;Usage: %s &lt;port&gt;&quot;, argv[0]);
          exit(0);    
      }
  
  int port = atoi(argv[1]);
  int sockfd;
  struct sockaddr_in serverAddr, newAddr;
  char buffer[1024];
  socklen_t addr_size;
  
  int frame_id=0;
  Frame frame_recv;
  Frame frame_send;   
  
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  
  memset(&amp;serverAddr, '\0', sizeof(serverAddr));
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(port);
  serverAddr.sin_addr.s_addr = inet_addr(&quot;127.0.0.1&quot;);
  
  bind(sockfd, (struct sockaddr*)&amp;serverAddr, sizeof(serverAddr));
  addr_size = sizeof(newAddr);
  
  while(1){
      int f_recv_size = recvfrom(sockfd, &amp;frame_recv, sizeof(Frame), 0, (struct sockaddr*)&amp;newAddr, &amp;addr_size);
      if (f_recv_size &gt; 0 &amp;&amp; frame_recv.frame_kind == 1 &amp;&amp; frame_recv.sq_no == frame_id){
          printf(&quot;[+]Frame Received: %s\n&quot;, frame_recv.packet.data);
          
          frame_send.sq_no = 0;
          frame_send.frame_kind = 0;
          frame_send.ack = frame_recv.sq_no + 1;
          sendto(sockfd, &amp;frame_send, sizeof(frame_send), 0, (struct sockaddr*)&amp;newAddr, addr_size);
          printf(&quot;[+]Ack Send\n&quot;);
      }else{
          printf(&quot;[+]Frame Not Received\n&quot;);
      }
      frame_id++; 
  }
  
  close(sockfd);
  return 0;
  }
  </code></pre>
  <p>Please help me. I am a beginner and taking references but not able to achieve.</p>