0

Server machine has more than one IP where one IP is connected with local LAN and Other is connected with WAN(Internet).

Server Application need to bind the IP and Port so that it can listen to the Client. I am able to get the All Ip in ther server machine but facing problem in finding the the active IP i.e. IP connected internet.

following code will give all the IP in the system.

 char chHostName[MAX_PATH];

  if ( gethostname(chHostName, sizeof(chHostName)) == SOCKET_ERROR)
  {
      return ;
  }    
struct hostent *phe = gethostbyname(chHostName);

  if (phe == 0)
  {    
      return ;

  }
//struct in_addr addrLocalIP;

  for (int i = 0; phe->h_addr_list[i] != 0; ++i) 
  {

    memcpy(&m_addrLocal.sin_addr.S_un, phe->h_addr_list[i], sizeof(struct in_addr));        
  } 

1. Is there any way to find the IP which is connected with internet among many IP in the system.
2. Is last IP fetched through the above program is always the IP which is connected with Internet?

Vikram Ranabhatt
  • 7,268
  • 15
  • 70
  • 133
  • For 1. you would have to parse the routing table and make the assumption that the default gateway is the uplink (it usually is) 2. you can never really tell without a traceroute on a particular interface, I reckon. – 0xC0000022L Apr 26 '11 at 11:56
  • There is no way to know. Zero or more IPs may be routable to the internet depending on path and routing tables that are outside the program's control. – Joe Apr 26 '11 at 12:00

2 Answers2

0

See Why no “what's my ip” well-known port/service? and related answers

Community
  • 1
  • 1
e.dan
  • 7,275
  • 1
  • 26
  • 29
0

Sockets don't know what they are being used for. Your application will have to get the IP address of the internet NIC from a configuration file or similar.