title: sendto: Network unreachable
body: <p>I have two machines I'm testing my code on, one works fine, the other I'm having some problems and I don't know why it is.</p>
  
  <p>I'm using an object (C++) for the networking part of my project.  On the server side, I do this: (error checking removed for clarity)</p>
  
  <pre><code>     res = getaddrinfo(NULL, port, &amp;hints, &amp;server)) &lt; 0
  
       for(p=server; p!=NULL; p=p-&gt;ai_next){
               fd = socket(p-&gt;ai_family, p-&gt;ai_socktype, p-&gt;ai_protocol);
               if(fd&lt;0){
                       continue;
                       }
  
               if(bind(fd, p-&gt;ai_addr, p-&gt;ai_addrlen)&lt;0){
                       close(fd);
                       continue;
                       }
               break;
               }
  </code></pre>
  
  <p>This all works.  I then make an object with this constructor</p>
  
  <pre><code>net::net(int fd, struct sockaddr *other, socklen_t *other_len){
          int counter;
           this-&gt;fd = fd;
           if(other != NULL){
                   this-&gt;other.sa_family = other-&gt;sa_family;
                   for(counter=0;counter&lt;13;counter++)
                           this-&gt;other.sa_data[counter]=other-&gt;sa_data[counter];
                   }
           else
                   cerr &lt;&lt; "Networking error" &lt;&lt; endl;
           this-&gt;other_len = *other_len;
           }
  
   void net::gsend(string s){   
           if(sendto(this-&gt;fd, s.c_str(), s.size()+1, 0, &amp;(this-&gt;other), this-&gt;other_len)&lt;0){
                   cerr &lt;&lt; "Error Sending, " &lt;&lt; s &lt;&lt; endl;
                   cerr &lt;&lt; strerror(errno) &lt;&lt; endl;
                   }
           return;
           }
  
    string net::grecv(){
           stringstream ss;
           string s;
           char buf[BUFSIZE];
           buf[BUFSIZE-1] = '\0';
  
           if(recvfrom(this-&gt;fd, buf, BUFSIZE-1, 0, &amp;(this-&gt;other), &amp;(this-&gt;other_len))&lt;0){
                   cerr &lt;&lt; "Error Recieving\n";
                   cerr &lt;&lt; strerror(errno) &lt;&lt; endl;
                   }
  
           // convert to c++ string and if there are multiple trailing ';' remove them
           ss &lt;&lt; buf;
           s=ss.str();
           while(s.find(";;", s.size()-2) != string::npos)
                   s.erase(s.size()-1,1);
           return s;
           }    
  </code></pre>
  
  <p>So my problem is, is that on one machine, everything works fine.  On another, everything works fine until I call my server's gsend() function.  In which I get a "Error: Network Unreachable."   I call gercv() first before calling gsend() too.  Can anyone help me? I would really appreciate it.</p>
  
  <p>SOLUTION</p>
  
  <p>It turns out that the server didn't like the way I set up the initial sockaddr structure. I was doing this:</p>
  
  <pre><code>     struct addrinfo hints;
       memset(&amp;hints, 0, sizeof(struct addrinfo));
       hints.ai_family = AF_UNSPEC;
       hints.ai_socktype = SOCK_DGRAM;
       hints.ai_flags = AI_PASSIVE;
       hints.ai_protocol = IPPROTO_UDP;
  </code></pre>
  
  <p>When it should have been like this</p>
  
  <pre><code>     struct addrinfo hints;
       memset(&amp;hints, 0, sizeof(struct addrinfo));
       hints.ai_family = AF_UNSPEC;
       hints.ai_socktype = SOCK_DGRAM;
  </code></pre>
  
  <p>Can anyone explain this?</p>