Linux C programming to get Client IP always get 0.0.0.0
<p>When i try to get client IP,I always get 0.0.0.0 when program running in CentOS,But when the some code running in Ubuntu,It will get the right IP :127.0.0.1
I don't understand that's why.</p>

<p>Please pardon My English.</p>

<p>Here is the code one:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;sys/types.h&gt;          /* See NOTES */
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
 #include &lt;arpa/inet.h&gt;
//#include &lt;netinet/ip.h&gt; /* superset of previous */
#define BUF_LEN 1024


int main(int argc, char const *argv[])
{
    int sockfd=0;
    int bind_ret=0;
    int listen_ret=0;
    int accept_ret=0;
    int read_ret=0;
    int write_ret=0;
    socklen_t addr_len=0;

    char buf[BUF_LEN+1]={0};

    struct sockaddr_in client_addr;
    struct sockaddr_in ser_addr ={
                        .sin_family= AF_INET,/* address family: AF_INET */
                        .sin_port=htons(8888),   /* port in network byte order */
                        .sin_addr.s_addr=inet_addr("127.0.0.1"),  /* internet address */
                        };  
//////////////////socket//////////////////////////////////
    sockfd=socket(AF_INET ,SOCK_STREAM,0);
    if (0&gt;sockfd){
        perror("socket");
        return -1;
    }
////////////////////bind/////////////////////////////////
    bind_ret=bind(sockfd, (const struct sockaddr *)&amp;ser_addr,sizeof(ser_addr));
    if(0&gt;bind_ret){
        perror("bind");
        return -1;
    }
//////////////////listen//////////////////////////////////
    listen_ret=listen(sockfd,10);
    if(0&gt;listen_ret){
        perror("listen");
        return -1;
    }
//////////////////accept//////////////////////////////////
    accept_ret=accept(sockfd,(struct sockaddr *)&amp;client_addr,&amp;addr_len);
    if(0&gt;accept_ret){
        perror("accept");
        return -1;
    }
//////////////////operation_data/////////////////////////////// 
    while(read_ret=read(accept_ret,buf,1024)){      //read data,until no data or error
        if(read_ret&lt;0) {
            perror("Socket read failed");
            return -5;
        }

        printf("receive:%s\n", buf);
        write_ret=write(accept_ret,buf,read_ret);
        if(write_ret!=read_ret){
            perror("write");
            return -1;
        }
    }
    //char *inet_ntoa(client_addr.sin_addr);
    char * ip_add =inet_ntoa(client_addr.sin_addr);
    int port=client_addr.sin_port;

    printf("IP:%s  PORT:%d\n", ip_add,port);
    close(client_addr);
    close(sockfd);
    return 0;
}
</code></pre>

<p>And code two:</p>

<pre><code>  #include &lt;stdio.h&gt;
#include &lt;sys/types.h&gt;          /* See NOTES */
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
 #include &lt;arpa/inet.h&gt;
#define BUF_LEN 1024

int main(int argc,char const **argv){
    int sockfd=0;
    int all_ret=0;
    socklen_t addr_len=0;

    char buf[BUF_LEN+1]={0};


    struct sockaddr_in addr={
                        .sin_family=AF_INET,/* address family: AF_INET */
                        .sin_port=htons(8888),   /* port in network byte order */
                        .sin_addr.s_addr=inet_addr("127.0.0.1"), /* internet address */
                        };
    char * ip_add =inet_ntoa(addr.sin_addr);
    printf("IP:%s \n", ip_add);
//////////////////socket//////////////////////////////////
    sockfd=socket(AF_INET ,SOCK_STREAM,0);
    if (0&gt;sockfd){
        perror("socket");
        return -1;
    }
//////////////////connection//////////////////////////////////
    // int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
    addr_len=sizeof(addr);
    all_ret=connect(sockfd, (const struct sockaddr *)&amp;addr,addr_len);
    if (0&gt;all_ret){
        perror("connect");
        return -1;
    }
//////////////////data send//////////////////////////////////
    all_ret=write(sockfd,"test_by_kevin",14);
    if (0&gt;all_ret){
        perror("write");
        return -1;
    }
    printf("write:%d\n",all_ret );
//////////////////data receive//////////////////////////////////
    all_ret=read(sockfd,buf,1024);
    if(all_ret&lt;0){
       perror("Socket read failed");
       return -1;
    }
    printf("%s\n",buf);

    close(sockfd);


}
</code></pre>