title: UDP send behaviour after connect()
body: <pre><code>#include &lt;stdio.h&gt;  
  #include &lt;errno.h&gt;  
  #include &lt;stdlib.h&gt;  
  #include &lt;string.h&gt;  
  #include &lt;sys/types.h&gt;  
  #include &lt;sys/socket.h&gt;  
  #include &lt;netinet/in.h&gt;  
  #include &lt;arpa/inet.h&gt;  
  
  int main()  
  {    
      struct sockaddr_in addr;  
      int fd, cnt,ret;  
      char ch = 'y',msg[] ="How are you";  
  
      if ((fd=socket(AF_INET,SOCK_DGRAM,0)) &lt; 0) {  
          printf("Error: socket");  
          exit(1);  
      }
      printf("\nDone socket\n");  
  
      /* set up destination address */  
      memset(&amp;addr,0,sizeof(addr));  
      addr.sin_family=AF_INET;  
      addr.sin_addr.s_addr=inet_addr("128.88.143.113");  
      addr.sin_port=htons(9090);  
  
      ret=connect(fd,(struct sockaddr *)&amp;addr,sizeof(addr));  
      perror("Connect:");  
  
      while(ch == 'y'){  
          cnt =  send(fd,msg,sizeof(msg),0);  
          if(cnt &lt; 0)  
          perror("send:");  
          printf("\nNumber of bytes sent = %d , \n",cnt);  
          printf("Continue (y/n)\n");  
          scanf(" %c",&amp;ch);  
  
       }
  
       return 0;  
  }  
  </code></pre>
  
  <p>The above code is compiled to run on a Linux machine.</p>
  
  <p>Assume that the above code sends data to a machine at IP address <code>128.88.143.113</code>. No UDP socket is bound to port <code>9090</code> at <code>128.88.143.113</code>.</p>
  
  <p>In the <code>while</code> loop, the first call to <code>send()</code> succeeds(the packet actually goes out on the wire; checked it using <code>trace</code>) and the second <code>send()</code> fails with <code>Connection refused</code>. The <code>third send()</code> succeeds and the forth fails and so on.</p>
  
  <p>I suspect that after first <code>send()</code> the stack receives an ICMP error message(seen in <code>tcpdump</code> on the Linux machine) which is saved in the socket structure. The second <code>send()</code> fails upon seeing this error and no packet is actually sent out. The second <code>send()</code> also clears the error in the socket structure. Therefore the third <code>send()</code> succeeds and the forth fails and so on.</p>
  
  <p>Questions:</p>
  
  <ol>
  <li>Is this assumption correct? </li>
  <li>What should be the correct behavior? Is there any RFC standard defining such behavior?</li>
  <li>Since UDP does not maintain any connection state, shouldn't every <code>send()</code> succeed?</li>
  </ol>