title: Sockets UDP: Using Sender Info from Recvfrom() in Sendto() Fails
body: <p>I'm trying to write a server that that receives a query via UDP and sends a response via UDP. The problem I'm running into is that when I try to send my response, I get a "101 Network is unreachable" error.</p>
  
  <p>Based on the question <a href="https://stackoverflow.com/questions/2782259/sendto-network-unreachable">here</a> I tried to resolve this by taking out the <code>hints.ai_flags = AI_PASSIVE;</code> line, but then the server never successfully gets the message. I looked at the man page for <code>addrinfo</code> and it sounds like if the <code>AI_PASSIVE</code> flag is set, the socket can only <code>recvfrom()</code>, while if it's not set, the socket can only <code>sendto()</code>. I've seen a bunch of examples online with people doing both from a single socket, though; what am I missing?</p>
  
  <p>Relevant code:</p>
  
  <pre><code>struct addrinfo hints;
  struct addrinfo *serverinfo;
  memset(&amp;hints,0,sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = AI_PASSIVE;
  
  int status = getaddrinfo(NULL, port, &amp;hints, &amp;serverinfo);
  int sock = socket(serverinfo-&gt;ai_family, serverinfo-&gt;ai_socktype, serverinfo-&gt;ai_protocol);
  status = bind(sock, serverinfo-&gt;ai_addr, serverinfo-&gt;ai_addrlen);
  freeaddrinfo(serverinfo);
  
  // poll until there's an incoming packet
  
  struct sockaddr sender;
  socklen_t sendsize = sizeof(sender);
  bzero(&amp;sender, sizeof(sender));
  
  recvfrom(sock, message, sizeof(message), 0, &amp;sender, &amp;sendsize);
  
  // message processing
  
  sendto(sock, response, sizeof(response), 0, (struct sockaddr *)&amp;sender, sendsize);
  </code></pre>
  
  <p>Yes, I have error checking for all those calls (that's how I found the problem); it's simply been left out here for brevity's sake.</p>