Thread: obtain hostname or IP

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    13

    obtain hostname or IP

    Hi all,

    I was just wondering if there was any way that I could obtain the hostname or IP address of the machine that my program will be running on?

    I have found examples to get the ip address, but i need to know the hostname.

    I have also found examples to get the hostname... but i need the ip. lol

    So is there any way of finding out one, without knowing the other?

    Hope this makes sense ;-)

    Cheers
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg." -- Bjarne Stroustrup

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A single computer may have more than one IP address and more than one hostname. Every computer replies to the loopback interface, 127.0.0.1. The hostname 'localhost' is usually bound to this.

    But take, for example, my router. In the internal network, it responds to 192.168.1.1, known as tuxserver, www.webloc.local, www.hpr.local, ...
    In the external network, it responds to 62.something, with the only DNS entry being some userid.myprovider.at or something, although one other used to work (but my IP changed).

    So it's far from definite. What you CAN try is to enumerate the network devices (but don't ask me how) and obtain the IP of each. Then you can do a reverse DNS lookup, it should give you more than one hostname.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The information you obtain from these functions may not be useable outside the local network, as described above.

    gethostname(): The gethostname function retrieves the standard host name for the local computer.

    gethostbyname(): The gethostbyname function retrieves host information (ip address) corresponding to a host name.

    getsockname(): The getsockname function retrieves the local name (ip address) for a connected socket.

    Your external IP address
    Last edited by anonytmouse; 12-01-2004 at 07:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to obtain NIC IP and MAC in C
    By kingsz1 in forum Networking/Device Communication
    Replies: 4
    Last Post: 10-13-2006, 11:29 AM
  2. Obtain local machine IP address
    By magic.mike in forum Windows Programming
    Replies: 4
    Last Post: 03-18-2005, 10:40 AM
  3. ip or hostname
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 04-08-2002, 08:41 PM