title: C UDP sockets: Arbitrary setsockopt behavior (with SO_RCVTIMEO)
body: <p>SO_RCVTIMEO is simply not creating a timeout condition in my output functions. I'm designing a file transfer service using a ARQ Sliding Window protocol. To keep things orderly, I'm starting with basic Stop-and-Wait.</p>
  
  <p>This is my initialization of timeval    </p>
  
  <pre><code>    struct timeval tv;
      tv.tv_sec = 0;
      tv.tv_usec = 100000;
  </code></pre>
  
  <p>This is my use of that structure and the socket in preparing a recvfrom timeout</p>
  
  <pre><code>if(setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &amp;tv, sizeof(tv))&lt;0)
  {
  fprintf(stderr, "Error in socket option for Timeout mechanism: %s",strerror(errno));
  close(sock);
  exit(EXIT_FAILURE);
  }
  
  if (-1 == bind(sock,(struct sockaddr *)&amp;sa, sizeof(sa)))
  {
      perror("error bind failed");
      close(sock);
      exit(EXIT_FAILURE);
  }
  </code></pre>
  
  <p>This is the body of code that is supposed to timeout:</p>
  
  <pre><code>for(;;)
  {
  ...
  while((read_bytes=fread((char*)&amp;(packet.body),sizeof(char),CHUNKSIZE,out_file))
  {
  ...
  while(1)
  {
  bytes_sent = sendto(sock, (struct sanpacket*)&amp;packet, sizeof(struct sanpacket), 0,(struct sockaddr*)&amp;sa, sizeof sa);
  if (bytes_sent &lt; 0) 
  {
      fprintf(stderr, "%s\n", strerror(errno));
      exit(EXIT_FAILURE);
  }
  printf("Waiting for ACK packet\n");
  
  recsize = recvfrom(sock, (struct sanpacket*)&amp;packet, sizeof(struct sanpacket), 0, (struct sockaddr*)&amp;sa, &amp;fromlen);
  if(errno == EAGAIN)
  {
      printf("Timed out; resending packet with sequence number %d", packet.sequence_num);
      continue;
  }
  if (recsize &lt; 0) 
  {
      fprintf(stderr, "%s\n", strerror(errno));
      exit(EXIT_FAILURE);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The problem: the timeout isn't firing, but setsockopt isn't returning any errors either. I have no way of knowing what I'm doing wrong.</p>
  
  <p>If there's anything else I can do to clarify my question or the environment, please let me know. And I'd appreciate it if you had any suggestions about debugging this.</p>