title: TCP C: Client not connecting using the IP address you entered?
body: <p>This is just a simple chat program wherein echoclient sends data to echoserver. In the example code given to us, why is it that the client wouldn't connect if I use an ip-address xxx.xxx.xxx.xxx (where xxx ranges from 0-255, for example 123.124.12.1) but it would if I input random numbers such as 12312 or 23423? In this case, when I print the network ordered ip, it gives</p>
  
  <blockquote>
  <pre><code>Network ordered ip(client side): 00000000
  Network ordered ip(server side): 127.0.0.1.
  </code></pre>
  </blockquote>
  
  <p>I read about 127.0.0.1 being the loopback ip address for localhost. But my question is, why is it that the client only connects with that address?</p>
  
  <p>When I run <code>./echoclient 12312</code>,
  the client connects and the program functions as it should. But if I run<code>./echoclient 123.123.12.1</code>, the client doesn't connect.</p>
  
  <p>..or I'm connecting the wrong way?</p>
  
  <p>Here's the code:</p>
  
  <p>echoserver.c</p>
  
  <pre><code>#include &lt;stdio.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;
  
  
  
  int main(int argc, char **argv)
  {
    int listenfd,connfd;
    struct sockaddr_in servaddr,cliaddr;
    socklen_t len = sizeof(cliaddr);
    char cli_ip[32];
    char mesg[1024];
  
    listenfd = socket(PF_INET,SOCK_STREAM,0);
    bzero(&amp;servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(54322);
    if ( bind( listenfd, (struct sockaddr*) &amp;servaddr, sizeof(servaddr) ) &lt; 0 ){
        perror(NULL);
        exit(-1);
    }
  
    //not present in udp server
    listen(listenfd,2);
  
    while(1){
        //not present in udp server
      connfd = accept(listenfd,(struct sockaddr *)&amp;cliaddr,&amp;len);
      inet_ntop(AF_INET,(struct in_addr *) &amp;cliaddr.sin_addr, cli_ip, sizeof(cli_ip) );
      printf("Client %s connected. \n",cli_ip);     
      while(1){
        memset(mesg,0,sizeof(mesg));
        if( recvfrom(connfd,mesg,sizeof(mesg),0,(const struct sockaddr *)&amp;cliaddr,&amp;len) &gt; 0 ){
              printf("From %s port %d: %s",cli_ip,ntohs(cliaddr.sin_port),mesg);
        }
        else {
          printf("Client %s disconnected. \n",cli_ip);      
          break;
        }
      }
      close(connfd);
    }
    close(listenfd);
    return 0;
  }
  </code></pre>
  
  <p>echoclient.c</p>
  
  <pre><code>#include &lt;stdio.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;
  
  
  
  int main(int argc, char **argv)
  {
    int sockfd;
    struct sockaddr_in servaddr;
    socklen_t len = sizeof(servaddr);
    char mesg[1024];
  
    if(argc!=2){ 
        printf("Usage: %s &lt;ip_addr&gt;\n",argv[0]);
        exit(1);
    }
  
    sockfd = socket(PF_INET,SOCK_STREAM,0);
    bzero(&amp;servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(54322);
    inet_pton(AF_INET,argv[1],&amp;servaddr.sin_addr);
    //printf("Network ordered ip: %08x\n",servaddr.sin_addr.s_addr);
  
    //not present in udp clients
    connect(sockfd,(struct sockaddr *)&amp;servaddr,sizeof(servaddr));
  
    while(1){
      fgets(mesg,sizeof(mesg),stdin);
      sendto(sockfd,mesg,strlen(mesg),0,(const struct sockaddr *)&amp;servaddr,len);
    }
    close(sockfd);
    return 0;
  }
  </code></pre>
  
  <p>Thank you in advance!</p>