From: HIToC <hitoc_mail@yahoo.it>
To: linux-c-programming@vger.kernel.org
Subject: Host IP address
Date: Tue, 22 Feb 2005 17:53:46 +0100 [thread overview]
Message-ID: <200502221751.44585.hitoc_mail@yahoo.it> (raw)
Hello all!
Sometimes is useful to know the IP address of the machine we are working on,
for example when we connect a SMTP server and do the EHLO <domain> command.
I have written a short C++ program that first prints the host name and the
host IP address, then if arguments from command line are passed prints
the IP address of each domain name.
/* IPadress.cpp
Author: HIToC
*/
#include <iostream.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <string>
#include <stdio.h>
int main(int argc, char* argv[])
{
hostent *hh;
string IP, my_name;
char* host_name = new char[64];
gethostname(host_name, 64);
my_name = host_name;
cout <<"Host name:\t\t" <<my_name <<'\n';
hh = gethostbyname(host_name);
IP = inet_ntoa(*((struct in_addr *)hh->h_addr));
cout <<"My IP address:\t\t" <<IP <<"\n\n";
for(int i = 1; i<argc; i++) {
hh = gethostbyname(argv[i]);
if(hh == NULL) {
cerr <<"Error with \"" <<argv[i] <<"\" !\n";
continue;
}
IP = inet_ntoa(*((struct in_addr *)hh->h_addr));
cout <<"IP address of " <<argv[i] <<" :\t" <<IP <<'\n';
}
return 0;
}
If called without any parameter, the output is always:
Host name: linux
My IP address: 127.0.0.2
If like argument I pass localhost, thw output is:
Host name: linux
My IP address: 127.0.0.2
IP address of localhost: 127.0.0.1
My intent is to know my real IP address!
Can anybody help me?
Thaks for any suggestion.
--
With regards,
HIToC
hitoc_mail@yahoo.it
next reply other threads:[~2005-02-22 16:53 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-02-22 16:53 HIToC [this message]
2005-02-23 0:26 ` Host IP address Glynn Clements
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200502221751.44585.hitoc_mail@yahoo.it \
--to=hitoc_mail@yahoo.it \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).