1

I have three process in three different computers.

Process 1, the client, asks Process 2 for the IP and PORT of Process 3.

Process 3 connected to Process 2 earlier, and Process 2 gets the IP of Process 3 from the file descriptor (Process 3 already knows the ip and port of Process 2).

This works fine, but if I try to run Process 2 and Process 3 in the same computer, the IP of Process 3 is always 127.0.0.1 so 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 is the code I'm using, and ipstr is the IP I get.

How can I solve this?

Many thanks!

Luke SpringWalker
  • 1,600
  • 3
  • 19
  • 33
  • If the IP is 127.0.0.1, you process 2 could look up the address of the local machine's network interfaces. But there could be more than one, it will have to pick one to send to process 1. – Barmar Jul 11 '13 at 00:20

3 Answers3

0

If after the getpeername() call for process 3 socket you detect that the address is the localhost, you can instead call getsockname() for the process 1 socket to get the IP process 1 used to connect to process 2. So long as process 3 is listening to the same interfaces as process 2 when they are running on the same machine, process 1 should be able to connect to process 3 with the 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);
}
jxh
  • 69,070
  • 8
  • 110
  • 193
  • The fact that Process 3 is bound to 127.0.0.1 means that it is NOT bound to a network-accessible interface, so Process 1 would not be able to communicate with Process 3 directly at all. – Remy Lebeau Jul 11 '13 at 05:40
  • It is more likely that process 3 is bound to INADDR_ANY but used localhost to connect to process 2. – jxh Jul 11 '13 at 07:34
  • That does not change what I said. The established connection would still be bound only to 127.0.0.1 once `connect()` is called, and thus would not be accessible to Process 1 over the network. – Remy Lebeau Jul 11 '13 at 09:41
0

I don't know how process 2 connect process 3:

int connect (int sockfd,struct sockaddr * serv_addr,int addrlen);

if the *serv_addr* argument in connect function is localhost, so process getpeername will get localhost, if the *serv_addr* is the IP of the PC,so you can get the IP of the PC.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
sundq
  • 735
  • 2
  • 9
  • 28
0

If Process 3 is using 127.0.0.1, and Process 1 is on a different machine, then Process 1 would never be able to communicate with Process 3, as Process 3 would not have access to the network to begin with because 127.0.0.1 is isolated to just the local machine only. Process 3 would have to be using the machine's actual network IP instead in order to be reachable by other machines on the network.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770