title: "No route to host" error when client sends request to server (C Networking)
body: <p>I have a basic client/server setup in C which works only if the client and the server are on the same machine. Is there any reason why my client wouldn't be able to send a request to my server if they are on different machines? </p>
  
  <p>In this instance I am running my server on a virtual machine and trying to run the client on my host machine.  </p>
  
  <p>Observations: </p>
  
  <ul>
  <li><p>If I start my server on my VM, and also run the client on the VM and change its address 
  in the client code so that there is a line that says </p>
  
  <p>addr.sin_addr.s_addr = inet_addr("<strong>127.0.0.1</strong>");</p>
  
  <p>then my server is able to connect with my client. There is no error here and everything occurs as it should.</p></li>
  <li><p>If I start my server on my VM, but then run my client on my host machine with my VM's IP instead: </p>
  
  <p>addr.sin_addr.s_addr = inet_addr("<strong>172.16.225.130</strong>");</p>
  
  <p>then this causes an error. The output of trying to run my client as my server runs is: </p>
  
  <p><strong>connect error: No route to host</strong></p></li>
  <li><p><strong>NOTE:</strong> I am able to ping my VM at 172.16.225.130 from my host machine and get a response successfully. But I am NOT able to telnet my VM successfully. It produces the same "No route to host" error. </p></li>
  </ul>
  
  <p><strong>Server code on virtual machine with IP: 172.16.225.130</strong></p>
  
  <pre><code>int main() {
  
    int sfd, cfd;
    int ch = 'k';
    int optval = 1;
    /* sockaddr_in holds IP address and port info*/
    struct sockaddr_in saddr, caddr;
  
  
    sfd = socket(AF_INET, SOCK_STREAM, 0);
  
    /* set struct sockaddr_in saddr fields */
    saddr.sin_family = AF_INET; /* Set address family to IPv4 Internet */
    saddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any internet address */
    saddr.sin_port = htons(29008); /* Set server port to 29008 */
  
    setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &amp;optval, sizeof optval);
  
    /* Bind address to the socket (sfd) created */
    bind(sfd, (struct sockaddr *)&amp;saddr, sizeof(saddr));
  
    listen(sfd, 1);
  
    while (1) {
  
          printf("Server waiting...\n");
  
          cfd = accept(sfd, (struct sockaddr *)NULL, NULL);
  
          /* Read character from client */
          if (read(cfd, &amp;ch, 1) &lt; 0) perror("read");
  
          printf("Read character: %c\n from client\n", ch);
  
          ch++;
  
          /* Send incremented character to client */
          if (write(cfd, &amp;ch, 1) &lt; 0) perror ("write");
  
          printf("Sent character: %c\n to client\n", ch);
  
          /* Close connection with client by closing the client fd */
          close(cfd);
    }
  
    return 0;
  }
  </code></pre>
  
  <p><strong>Client code on my host machine</strong></p>
  
  <pre><code>int main() {
  
    int cfd;
  
    struct sockaddr_in addr;
    char ch = 'r';
  
    cfd = socket(AF_INET, SOCK_STREAM, 0);
  
    addr.sin_family = AF_INET;
  
  
    addr.sin_addr.s_addr = inet_addr("172.16.225.130");
  
    addr.sin_port = htons(29008);
  
  
    if (connect(cfd, (struct sockaddr *)&amp;addr, sizeof(addr)) &lt; 0) {
          perror("connect error");
          return -1;
    }
  
  
    if (write(cfd, &amp;ch, 1) &lt; 0) perror("write");
    if (read(cfd, &amp;ch, 1) &lt; 0) perror("read");
    printf("nReply from Server: %cnn\n", ch);
    close(cfd);
    return 0;
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> 
  Here are things I've tried already and still do not work. </p>
  
  <p><strong>1.</strong> Opened up port 29008 in my VM:</p>
  
  <p>Appended rule in my /etc/sysconfig/iptables file:</p>
  
  <p>-A INPUT -m state --state NEW -m tcp -p tcp --dport 29008 -j ACCEPT</p>
  
  <p>Saved and closed the file. Restarted iptables:</p>
  
  <p>/etc/init.d/iptables restart</p>
  
  <p><strong>2.</strong> Allowed requests to be received by each other by configuring firewall settings:</p>
  
  <p>On VM: sudo iptables -A INPUT -s 172.16.225.1 -j ACCEPT</p>
  
  <p>On host: sudo iptables -A INPUT -s 172.16.225.130 -j ACCEPT</p>
  
  <p><strong>3.</strong> Turned off my iptables on my VM completely. This produced a different result. Instead of a "No route to host" error occuring, running my client causes it to just hang. </p>