title: Client server connection on loopback interface
body: <p>I'm trying to test a client-server simple implementation using localhost address.
  Here's the code.</p>
  
  <p>Server:</p>
  
  <pre><code>/*
   * Sequential busy-waiting
   */
  int main(int argc, char** argv) {
  
      int opt, client_addr_l, errsv;
      unsigned short port;
      struct sockaddr_in server_addr, client_addr;
  
      /* ... */
  
      printf("Port number retrieved (%d), server is starting ...\n", port);
  
      /*TCP Socket creation*/
      sock_ds = socket(AF_INET, SOCK_STREAM, 0); 
      if(sock_ds == -1){
          fprintf(stderr, "Socket creation error: %s\n", strerror(errno));
          exit(EXIT_FAILURE);
      }
  
      /*Server address binding*/
      memset(&amp;server_addr, 0, sizeof(server_addr));
      server_addr.sin_family = AF_INET;
      server_addr.sin_port = htons(port);
      server_addr.sin_addr.s_addr = INADDR_ANY;
  
     /*!!!! */
     int optval = 1;                                      
     if( (setsockopt(sock_ds,SOL_SOCKET,SO_REUSEADDR,&amp;optval,sizeof(optval))) == -1 ) {                
         printf("Error on setsockopt\n");                         
         exit(EXIT_FAILURE);                                  
     }
    /*????*/
  
     if(bind(sock_ds, (struct sockaddr*)&amp;server_addr, sizeof(server_addr)) == -1){
         fprintf(stderr, "Address binding error\n");
         exit(EXIT_FAILURE);
     }
  
     /*Server with passive socket*/
     if(listen(sock_ds, SOMAXCONN) == -1){
         fprintf(stderr, "Listen call error: %s\n", strerror(errno));
         exit(EXIT_FAILURE);
     }
  
     while(1){
         memset(&amp;client_addr, 0, sizeof(client_addr));
         acc_sock_ds = accept(sock_ds, (struct sockaddr *)&amp;client_addr, &amp;client_addr_l);
         printf("DEBUG: LINE201, acc_sock_ds = %d\n", acc_sock_ds);
         /*Connect error management*/
         if(acc_sock_ds == -1){
             fprintf(stderr, "Fatal error on accept %d(%s)\n"
                     , errsv, strerror(errsv));
              exit(EXIT_FAILURE);
         }
  
         //sin_addr to ASCII (string) );
         printf("Connected with: %s\n", inet_ntoa(client_addr.sin_addr)); 
  
         /*...*/     
  
         close(acc_sock_ds);
  
        /*...*/
      }
  
  /*...*/
  
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code> int main(){
  
      int sock_ds;
      struct sockaddr_in remote_addr;
      struct hostent *hp;
  
      /*TCP Socket creation*/
      sock_ds = socket(AF_INET, SOCK_STREAM, 0); 
      if(sock_ds == -1){
          fprintf(stderr, "Socket creation error\n");
          exit(EXIT_FAILURE);
      }
  
      remote_addr.sin_family = AF_INET;
      remote_addr.sin_port = htons(25556);
  
      hp = gethostbyname("localhost");
      bcopy(hp -&gt; h_addr, &amp;remote_addr.sin_addr, hp -&gt; h_length); //fills address entry
  
     if(connect(sock_ds, (struct sockaddr*)&amp;remote_addr, sizeof(remote_addr)) == -1){ //connection attempt
         fprintf(stderr, "Connect failure(%s)\n", strerror(errno));
         exit(EXIT_FAILURE);        
     }
  
     /*...*/
  
  }
  </code></pre>
  
  <p>When i run them on two different terminals server returns me:</p>
  
  <pre><code>Port number retrieved (25556), server is starting ...
  Server is ready. Waiting for client connections.
  DEBUG: LINE201, acc_sock_ds = 4
  Connected with: 0.0.0.0
  </code></pre>
  
  <p>My question is: why does the client address retrieved by the server is 0.0.0.0.  It should not be 127.0.0.1?</p>