Thread: how to get hostname and IP address of a machine

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    10

    how to get hostname and IP address of a machine

    Hi,

    I want to get the hostname and the ip address of the machine in C on both windows and linux environments. Are there any functions in C which will give me this information?

    Thanks,
    Chandana.
    Last edited by Chandana; 08-17-2009 at 07:54 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    gethostname(), gethostbyaddr(), gethostbyname().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    10
    Thanks for the reply.
    Can you give me some sample code which uses this gethostname(). I am trying to call gethostname() from the main function, but I am getting "segmentation fault" error.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Post your code that is giving you the seg fault.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Chandana View Post
    Thanks for the reply.
    Can you give me some sample code which uses this gethostname(). I am trying to call gethostname() from the main function, but I am getting "segmentation fault" error.
    As a first guess I'd say that you don't have a big enough buffer for the hostname which is stored in the first argument of gethostname().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. obtain hostname or IP
    By afisher in forum Networking/Device Communication
    Replies: 3
    Last Post: 12-01-2004, 07:46 AM
  2. Retrieving current PC IP Address? Please help...
    By SyntaxBubble in forum Networking/Device Communication
    Replies: 4
    Last Post: 09-02-2003, 06:12 PM
  3. Outputting your IP and hostname
    By Nakeerb in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2002, 08:24 PM
  4. ip or hostname
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 04-08-2002, 08:41 PM