sockets - C - Public IP from file descriptor -


i have 3 process in 3 different computers.

process 1, client, asks process 2 ip , port of process 3.

process 3 connected process 2 earlier, , process 2 gets ip of process 3 file descriptor (process 3 knows ip , port of process 2).

this works fine, if try run process 2 , process 3 in same computer, ip of process 3 127.0.0.1 process 1 never finds process 3.

    socklen_t len;     struct sockaddr_storage addr;     char ipstr[inet_addrstrlen];      len = sizeof addr;     getpeername(fd, (struct sockaddr*) &addr, &len);      struct sockaddr_in *s = (struct sockaddr_in *) &addr;      inet_ntop(af_inet, &s->sin_addr, ipstr, sizeof ipstr); 

this code i'm using, , ipstr ip get.

how can solve this?

many thanks!

if after getpeername() call process 3 socket detect address localhost, can instead call getsockname() process 1 socket ip process 1 used connect process 2. long process 3 listening same interfaces process 2 when running on same machine, process 1 should able connect process 3 same address.

len = sizeof addr; getpeername(p3_socket, (struct sockaddr*) &addr, &len); struct sockaddr_in *s = (struct sockaddr_in *) &addr; inet_ntop(af_inet, &s->sin_addr, ipstr, sizeof ipstr);  if (strcmp(ipstr, "127.0.0.1") == 0) {     len = sizeof(addr);     getsockname(p1_socket, (struct sockaddr *)addr, &len);     inet_ntop(af_inet, &s->sin_addr, ipstr, sizeof ipstr); } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -