C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 08-17-2009, 07:45 AM   #1
Registered User
 
Join Date: Aug 2009
Posts: 5
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.
Chandana is offline   Reply With Quote
Old 08-17-2009, 08:09 AM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
gethostname(), gethostbyaddr(), gethostbyname().
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 08-17-2009, 08:58 AM   #3
Registered User
 
Join Date: Aug 2009
Posts: 5
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.
Chandana is offline   Reply With Quote
Old 08-17-2009, 09:10 AM   #4
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Post your code that is giving you the seg fault.
__________________
bit∙hub [bit-huhb] n. A source and destination for information.
bithub is offline   Reply With Quote
Old 08-17-2009, 11:56 AM   #5
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
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().
itCbitC is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 04:10 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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