title: recvfrom in socket programming with C
body: <p>So I was trying to understand socket programming in C when I came across this code:</p>
  
  <pre><code>/* Sample UDP client */
  
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc, char**argv)
  {
      int sockfd,n;
      struct sockaddr_in servaddr;
      char sendline[] = "Hello UDP server! This is UDP client";
      char recvline[1000];
  
      if (argc != 2)
      {
          printf("usage:  ./%s &lt;IP address&gt;\n",argv[0]);
          return -1;
      }
  
      sockfd=socket(AF_INET,SOCK_DGRAM,0);
  
      bzero(&amp;servaddr,sizeof(servaddr));
      servaddr.sin_family = AF_INET;
      servaddr.sin_addr.s_addr=inet_addr(argv[1]);
      servaddr.sin_port=htons(32000);
  
      sendto(sockfd,sendline,strlen(sendline),0,(struct sockaddr *)&amp;servaddr,sizeof(servaddr));
      n=recvfrom(sockfd,recvline,10000,0,NULL,NULL);
      recvline[n]=0;
      printf("Received: %s\n",recvline);
      return 0;
  }
  </code></pre>
  
  <p>It seems that the <code>recvfrom()</code> call does not need an ip address to send the message. A look at the man pages revealed the following lines:</p>
  
  <blockquote>
    <p>If src_addr is not NULL, and the underlying protocol provides the
    source address, this source address is filled  in. When  src_addr  is
    NULL,  nothing  is  filled  in;  in this case, addrlen is not used,
    and should also be NULL.</p>
  </blockquote>
  
  <p>So I think that the underlying protocol provides the source IP address. My problem is, how does it really figure out the address to receive the message from ? Is it that, once you send a message to an address, you cannot use the same socket to send messages to other addresses ? So that it keeps on using the same address ?</p>
  
  <p>Please help. Couldn't find an answer anywhere in Google or any lecture note. </p>
  
  <p>Thank you in advance. </p>