CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    gethostbyname returns more than one IP

    I'm using gethostbyname to get IP address by a computer name on a LAN, but sometimes it returns more than one IP for one name. But how can that be? And if that happens how to get _The_ IP to be used to connect to that particular PC?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: gethostbyname returns more than one IP

    Perhaps, your computer has more than one network interface cards?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: gethostbyname returns more than one IP

    Yes, that's true, Victor. But how in that case to determine which IP should be used to connect to PC that's calling gethostbyname?

  4. #4
    Join Date
    Jul 2005
    Posts
    266

    Re: gethostbyname returns more than one IP

    You can check the IP, if you need a local address you can check whether your ip is:
    Class A: 10.*.*.*

    Class B: 172.*.*.*
    or
    Class C: 192.*.*.*

    All of these Classes are LAN IP address types, if your IP doesnt fit any of these cases it means its an external WAN IP that people outside of your local network can connect to using the internet.

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: gethostbyname returns more than one IP

    Quote Originally Posted by kolkoo View Post
    You can check the IP, if you need a local address you can check whether your ip is:
    Class A: 10.*.*.*

    Class B: 172.*.*.*
    or
    Class C: 192.*.*.*

    All of these Classes are LAN IP address types, if your IP doesnt fit any of these cases it means its an external WAN IP that people outside of your local network can connect to using the internet.
    This is by no means a hard-fast rule.

    As to the OP's question, the first entry in h_addr_list should be the preferred address. A DNS can return multiple addresses for the same hostname according to the priority. gethostbyname() will order them in the priority order so just use the first entry.

  6. #6
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: gethostbyname returns more than one IP

    Quote Originally Posted by hoxsiew View Post
    the first entry in h_addr_list should be the preferred address. A DNS can return multiple addresses for the same hostname according to the priority. gethostbyname() will order them in the priority order so just use the first entry.
    I thought that too, but unfortunately that's not always true. Here's my situation (see diagram attached):

    If gethostbyname is ran from PC#1, it returns IPs in the following order:
    1. For PC #3
    2. For PC #2
    3. For Wireless adapter from PC #2

    If I take IP 1 it will direct me to PC #3 that I will not even be able to connect to (both LAN connections are done via cross-over ethernet cable and two network cards in PC #2).

    So how can I resolve this situation?

    As for kolkoo's suggestion, I agree that would not be a good way to hardcode it like that, since for sure it will not hold up on Vista with the introduction of IP6 addresses.
    Attached Images Attached Images  

  7. #7
    Join Date
    Feb 2005
    Posts
    2,160

    Re: gethostbyname returns more than one IP

    Sounds like you've got a non-standard setup at best. Your DNS must be really messed up too to give hostnames out in that manner. I'm afraid I've got no answer for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured