<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WiredRevolution.com &#187; hostent</title>
	<atom:link href="http://www.wiredrevolution.com/tag/hostent/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wiredrevolution.com</link>
	<description>A Bit of Linux Wisdom</description>
	<lastBuildDate>Wed, 18 Jan 2012 22:45:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Display hostname and IP address in C</title>
		<link>http://www.wiredrevolution.com/c/display-hostname-and-ip-address-in-c?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=display-hostname-and-ip-address-in-c</link>
		<comments>http://www.wiredrevolution.com/c/display-hostname-and-ip-address-in-c#comments</comments>
		<pubDate>Sat, 18 Oct 2008 12:54:53 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[gethostbyname]]></category>
		<category><![CDATA[gethostname]]></category>
		<category><![CDATA[hostent]]></category>
		<category><![CDATA[hostname]]></category>
		<category><![CDATA[inet_ntoa]]></category>
		<category><![CDATA[IP address]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=394</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/c_icon.png" width="80" height="80" alt="" title="C" /><br/>Here is a good way to determine the hostname and IP address of the local machine in C. You first have to grab the hostname with gethostname(). char hostbuf[256]; gethostname(hostbuf,sizeof(hostbuf)); Take the hostname and use it to grab the hostent struct with gethostbyname(). struct hostent *hostentry; hostentry = gethostbyname(hostbuf); Finally you have to take the [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/c/find-ip-address-from-remote-end-of-a-tcp-socket' rel='bookmark' title='Permanent Link: Find IP address from remote end of a TCP socket'>Find IP address from remote end of a TCP socket</a></li>
<li><a href='http://www.wiredrevolution.com/red-hat/change-hostname-on-redhat' rel='bookmark' title='Permanent Link: Change hostname on RedHat'>Change hostname on RedHat</a></li>
<li><a href='http://www.wiredrevolution.com/c/format-output-using-printf' rel='bookmark' title='Permanent Link: Format output using printf'>Format output using printf</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/c_icon.png" width="80" height="80" alt="" title="C" /><br/><p>Here is a good way to determine the hostname and IP address of the local machine in C.</p>
<p>You first have to grab the hostname with <strong>gethostname</strong>().</p>
<pre>
char hostbuf[256];
gethostname(hostbuf,sizeof(hostbuf));
</pre>
<p>Take the hostname and use it to grab the hostent struct with <strong>gethostbyname</strong>().</p>
<pre>
struct hostent *hostentry;
hostentry = gethostbyname(hostbuf);
</pre>
<p>Finally you have to take the hostent that is returned and pull out the IP address.  It is in network byte order, so you have to convert it to a string with <strong>inet_ntoa</strong>().</p>
<pre>
char * ipbuf;
ipbuf = inet_ntoa(*((struct in_addr *)hostentry->h_addr_list[0]));
</pre>
<p>Put all this together into a working programs.</p>
<pre>
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;errno.h&gt;
#include &lt;netdb.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;

int main()
{
        char hostbuf[256];
        char * ipbuf;
        struct hostent *hostentry;
        int ret;

        ret = gethostname(hostbuf,sizeof(hostbuf));

        if(-1 == ret){
                perror("gethostname");
                exit(1);
        }

        hostentry = gethostbyname(hostbuf);

        if(NULL == hostentry){
                perror("gethostbyname");
                exit(1);
        }

        ipbuf = inet_ntoa(*((struct in_addr *)hostentry->h_addr_list[0]));

        if(NULL == ipbuf){
                perror("inet_ntoa");
                exit(1);
        }

        printf("Hostname: %s Host IP: %s\n", hostbuf, ipbuf);

        return 0;
}
</pre>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/c/find-ip-address-from-remote-end-of-a-tcp-socket' rel='bookmark' title='Permanent Link: Find IP address from remote end of a TCP socket'>Find IP address from remote end of a TCP socket</a></li>
<li><a href='http://www.wiredrevolution.com/red-hat/change-hostname-on-redhat' rel='bookmark' title='Permanent Link: Change hostname on RedHat'>Change hostname on RedHat</a></li>
<li><a href='http://www.wiredrevolution.com/c/format-output-using-printf' rel='bookmark' title='Permanent Link: Format output using printf'>Format output using printf</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/c/display-hostname-and-ip-address-in-c/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

