0

Possible Duplicate:
Linux / C++: Get the IP Address of local computer

I'm porting a software made in java with the and the local address for the socket is needed. I'm looking for a equivalent for the lines:

    Socket socket;
    ...
    InetAddress internal = socket.getLocalAddress();
    String localIp = internal.getHostAddress();
Community
  • 1
  • 1
RafaelLopes
  • 473
  • 2
  • 7

2 Answers2

2

The functions you are looking for are getsockname and inet_ntop.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • `code` struct sockaddr_in addr; size_t addr_len = sizeof(addr); if (getsockname(socketfd, (struct sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { ERROR_PRINT("Error getting local address"); return NULL; } return inet_ntoa(addr.sin_addr);`code` – RafaelLopes Nov 21 '11 at 19:58
0

Perhaps getpeername or getsockname ?

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547