Qt-interest Archive, May 2003
how to get the IP Address of my machine
Message 1 in thread
hi everybody,
how to get IP Address of my machine using QT.
I tried with QHostAddress QSocketDevice::address () const,
but it always returns 0.0.0.0 .
I even tried direct networking api :
char name[256];
struct hostent * host;
if(gethostname(name, 256) != 0)
{
printf("no host name........\n";
return ;
}
if((host = gethostbyname(name)) != 0)
{
printf("no host name........\n";
return FALSE;
}
i am getting the host name, but gethostbyname() always returns
127.0.0.1. as ip address.
can any body tell me how to get my machine ip address.
thanks in advance,
Rajendra
-----------------------SOFTPRO DISCLAIMER------------------------------
Information contained in this E-MAIL and any attachments are
confidential being proprietary to SOFTPRO SYSTEMS is 'privileged'
and 'confidential'.
If you are not an intended or authorised recipient of this E-MAIL or
have received it in error, You are notified that any use, copyiny or
dissemination of the information contained in this E-MAIL in any
manner whatsoever is strictly prohibited. Please delete it immediately
and notify the sender by E-MAIL.
In such a case reading, reproducing, printing or further dissemination
of this E-MAIL is strictly prohibited and may be unlawful.
SOFTPRO SYSYTEMS does not REPRESENT or WARRANT that an attachment
hereto is free from computer viruses or other defects.
The opinions expressed in this E-MAIL and any ATTACHEMENTS may be
those of the author and are not necessarily those of SOFTPRO SYSTEMS.
------------------------------------------------------------------------
Message 2 in thread
>>>>> "Rajendra" == Rajendra P <rajendra.penmetsa@softprosys.com> writes:
Rajendra> hi everybody, how to get IP Address of my machine using
Rajendra> QT.
You did not explain why you want to do this. If it is to protect your
software against copy, it is a bad solution!
A machine does not have an IP address; it may have 0 IP adress, and it
usually have several IP addresses, one (at least) for each network
interfaces (it might even have several IP adresses on the same
physical network). So an IP address does not belong to a machine, but
to a network interface (and machines have several of them usually;
even a machine with a single Ethernet connection with a fixed address
IP on it has two network interfaces, the local loopback and the
ethernet)
On Linux, the "ifconfig -a" command displays information about each
network interfaces; on the machine on which I am typing this reply
(which happens to be also a small firewall between my Internet ADSL
connection and a private home network) it gives
# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:50:FC:57:6A:F3
inet addr:192.168.0.5 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1200 Metric:1
RX packets:2932925 errors:0 dropped:0 overruns:0 frame:0
TX packets:5225583 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:433022502 (412.9 MiB) TX bytes:1465149410 (1.3 GiB)
Interrupt:9 Base address:0xb800
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:1884030 errors:0 dropped:0 overruns:0 frame:0
TX packets:1884030 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:219448007 (209.2 MiB) TX bytes:219448007 (209.2 MiB)
ppp0 Link encap:Point-to-Point Protocol
inet addr:80.65.224.217 P-t-P:62.4.16.246 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1200 Metric:1
RX packets:143980 errors:0 dropped:0 overruns:0 frame:0
TX packets:96896 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:147841734 (140.9 MiB) TX bytes:6135119 (5.8 MiB)
As you can see, my system have three valid IP addresses: 192.168.0.5
(on the home Ethernet network), 127.0.0.1 (on the local loopback
interface), and 80.65.224.217 (on the ADSL connection, visible to the
outside Internet).
I don't know Windows, but I heard that the equivalent command under
Windows might be "IPCONFIG /ALL"
I'm not sure there is really a "main" IP adress, but most systems are
configured to have a "principal" hostname (which you can query thru
gethostname as you guessed), and it probably would be localhost on
many (perhaps badly configured) systems (in particular on most systems
with a dynamic IP address set up at dial up)!
In a recent message <87d6itkg3a.fld@barrow.com> dated 08 May 2003
00:17:13 -0800on comp.os.linux.development.apps Floyd Davidson
suggested the following code to get the IP address of the eth0
interface (I did not test it):
################################################################
// code by Floyd Davidson
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(void)
{
int sfd, i;
struct ifreq ifr;
struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
memset(&ifr, 0, sizeof ifr);
if (0 > (sfd = socket(AF_INET, SOCK_STREAM, 0))) {
perror("socket()");
exit(1);
}
strcpy(ifr.ifr_name, "eth0");
sin->sin_family = AF_INET;
if (0 == ioctl(sfd, SIOCGIFADDR, &ifr)) {
printf("%s: %s\n", ifr.ifr_name, inet_ntoa(sin->sin_addr));
}
return 0;
}
################################################################
You might use gethostid to get a pseudo-unique number for you host. I
don't advise this.
If you want software protection (as an opensource developer I might
not share this need) get a real tool for this (which might be a
nightmare both for your clients and for your support team).
--
[ signature omitted ]
Message 3 in thread
On Thursday 08 May 2003 05:08 am, Rajendra P wrote:
> hi everybody,
> how to get IP Address of my machine using QT.
Here's some code fragments to do it. Note that it's stolen from a larger
form, so this isn't going to compile for you, but it will give you the
general idea.
void FrmSettingsImpl::lookupIp(bool show_errors)
{
btn_discover->setEnabled(false);
QApplication::setOverrideCursor(Qt::waitCursor);
char hostname[1024];
if (gethostname(hostname, sizeof(hostname)) < 0) {
if (show_errors) {
QMessageBox::warning(this, "IP Discovery Error",
"Unable to find hostname.", "OK");
}
return;
}
/* start asynchronous name->IP resolution */
qDebug("Using hostname %s", hostname);
QDns *dns = new QDns(hostname);
QObject::connect(dns, SIGNAL(resultsReady()),
this, SLOT(dnsComplete()));
/* remember settings, in case an error occurs after we lookup the
* A record
*/
_lookup_ip_show_errors = show_errors;
}
/****************************************************************************\
* FrmSettingsImpl::dnsComplete(void) [slot]
\****************************************************************************/
void FrmSettingsImpl::dnsComplete(void)
{
btn_discover->setEnabled(true);
QApplication::restoreOverrideCursor();
QDns *dns = (QDns *)sender();
QValueList<QHostAddress> addrs = dns->addresses();
delete dns;
/* remove localhost if it was returned */
addrs.remove(QHostAddress(0x7f000001));
if (!addrs.size()) {
if (_lookup_ip_show_errors) {
QMessageBox::warning(this, "IP Discovery Error",
"Couldn't find an IP address "
"for this machine.", "OK");
}
return;
}
if (addrs.size() == 1) {
txt_server_name->setText(addrs[0].toString());
return;
}
/* multiple addresses, let user pick */
QStringList ip_list;
QValueList<QHostAddress>::const_iterator addr_it = addrs.begin();
for (; addr_it != addrs.end(); ++addr_it) {
ip_list << (*addr_it).toString();
}
QString ip_addr = QInputDialog::
getItem("Choose IP Address",
"Multiple IP addresses were discovered for this machine.\n"
"Please pick the most appropriate IP address to advertise.",
ip_list, 0, false, 0, this);
if (ip_addr != QString::null) {
txt_server_name->setText(ip_addr);
}
}
/****************************************************************************\
* FrmSettingsImpl::btnDiscover_Clicked(void) [slot]
\****************************************************************************/
void FrmSettingsImpl::btnDiscover_Clicked(void)
{
lookupIp(true); // show errors on interactive lookup
}
As somebody else already replied, do NOT use IP addresses for any licensing
purpose. Ethernet MAC addresses work best for that, but are not perfect by
any means. I have code for finding MAC addresses on different OSes,
including Linux, Solaris, Tru64, Irix, and Windows. You might find it
searching the list archives. If not, I can send it to you.
---
Geoffrey Wossum
Software Engineer
Long Range Systems - http://www.pager.net