How do you find the local IP without getting the loopback?

This is a discussion on How do you find the local IP without getting the loopback? within the C++ Programming forums, part of the General Programming Boards category; By using gethostname & gethostbyname you can get a machine's IP address, but on some machines, it seems to give ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    72

    How do you find the local IP without getting the loopback?

    By using gethostname & gethostbyname you can get a machine's IP address, but on some machines, it seems to give the loopback and on some, the actual IP (well, the loopback is a valid IP I suppose, but you know what I mean). Maybe this is attributed to the /etc/hosts file (in Linux)? In any case, is there a way to ensure you're getting back the actual machine IP as opposed to the loopback? Is there a way to test for multiple IPs on a machine?

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    72
    Originally posted by vVv
    > In any case, is there a way to ensure you're getting back the actual machine IP as opposed to the loopback?

    Once you have a connected socket, you can do;
    That is a viable option, and thank you. That said, is there a way do this without using sockets? I'd rather not add socket stuff in there if it is used only to find the IP (unless it's necessary).

  3. #3
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    You might try having your program run /sbin/ifconfig, then dissecting the output for the IP. Should just be a matter of finding the substring.

    This would help also if a machine was connected on more than one network (like a router, or a wireless laptop that happened to also be plugged in)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 03:49 PM
  2. getting local ip (not internet ip)
    By ipe in forum C Programming
    Replies: 12
    Last Post: 03-17-2003, 03:10 PM
  3. Replies: 8
    Last Post: 01-09-2003, 10:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21