title: printing client ip when server is on same machine
body: <p>I am writing a simple tcp echo client server code both my client and server code are running on the same machine i did bind two different addresses to client and server but client ip address is not getting printed when connected to server i changed addresses and still i was not able to print address i don't see any mistake in code.Is this my os issue or am i making some mistake.</p>
  
  <p>my server code:</p>
  
  <pre><code>    #include&lt;stdio.h&gt;
      #include&lt;stdlib.h&gt;
      #include&lt;sys/socket.h&gt;
      #include&lt;sys/types.h&gt;
      #include&lt;string.h&gt;
      #include&lt;netinet/in.h&gt;
  
      #define ERROR -1
      #define MAX_CLIENTS 10
      #define MAX_DATA 1024
  
      int main(int argc, char *argv[]){
  
          struct sockaddr_in server;
          struct sockaddr_in client;
  
          int sock;
          int new;
          int sockaddr_len = sizeof(struct sockaddr_in);
          int data_len;
          char data[MAX_DATA+1];
  
          if((sock = socket(AF_INET,SOCK_STREAM,0)) == ERROR){
  
              perror("server socket");
              exit(-1);
  
          }
  
          server.sin_family = AF_INET;
          server.sin_port = htons(atoi(argv[1]));
          inet_aton(argv[2],&amp;server.sin_addr.s_addr);
          bzero(&amp;server.sin_zero,0);
  
          if(bind(sock, (struct sockaddr *)&amp;server,sockaddr_len) == ERROR){
  
              perror("bind");
              exit(-1);
          }
  
          if(listen(sock, MAX_CLIENTS) == ERROR){
  
              perror("listen");
              exit(-1);
          }
  
          while(1){
  
              if((new = accept(sock,(struct sockaddr *)&amp;client,&amp;sockaddr_len)) == ERROR){ 
  
                  perror("accept");
                  exit(-1);
  
              }
  
      //ip not getting printed
              printf("New client connected from port no %d IP %s\n",ntohs(client.sin_port),inet_ntoa(client.sin_addr.s_addr));
  
      data_len = 1;
  
      while(data_len){
  
          data_len = recv(new,data,MAX_DATA,0);
  
          if(data_len){
  
              send(new, data, data_len,0);
              data[data_len] = '\0';
              printf("Sent message: %s", data);
  
  
          }
  
      }
  
      printf("Client disconnected\n");
      close(new);
  
  }
      }
  </code></pre>
  
  <p>my client code:</p>
  
  <pre><code>    #include&lt;stdio.h&gt;
      #include&lt;stdlib.h&gt;
      #include&lt;sys/socket.h&gt;
      #include&lt;sys/types.h&gt;
      #include&lt;string.h&gt;
      #include&lt;netinet/in.h&gt;
  
      #define ERROR -1
      #define BUFFER 1024
  
      int main(int argc, char *argv[]){
  
          struct sockaddr_in remote_server,client;
          int sock,len;
  
          char input[BUFFER];
          char output[BUFFER+1];
  
  
          client.sin_family = AF_INET;
          client.sin_port = htons(3000);
          inet_aton("127.0.2.8",&amp;client.sin_addr.s_addr);
          bzero(&amp;client.sin_zero,0);
  
          if((sock = socket(AF_INET,SOCK_STREAM,0)) == ERROR){
              perror("socket");
              exit(-1);
          }
  
          int size = sizeof(struct sockaddr_in);
  
          if(bind(sock, (struct sockaddr *)&amp;client,size) == ERROR){
  
              perror("bind");
              exit(-1);
          }
  
          remote_server.sin_family = AF_INET;
          remote_server.sin_port = htons(atoi(argv[2]));
          remote_server.sin_addr.s_addr = inet_addr(argv[1]); 
          bzero(&amp;remote_server.sin_zero , 0);
  
          if((connect(sock ,(struct sockaddr *)&amp;remote_server , sizeof(struct sockaddr_in))) == ERROR){
  
              perror("connect");
              exit(-1);
  
          }
  
  while(1){
  
      fgets(input, BUFFER, stdin);
      send(sock, input, strlen(input) , 0);
  
      len = recv(sock,output,BUFFER,0);
  
      output[len] = '\0';
  
      printf("%s\n",output);
  
  }
  
  close(sock);
  
   }
  </code></pre>
  
  <p>This is the output:
  New client connected from port no 3000 and IP</p>