Qt学习之路之获取本机IP

Qt提供的好几个类都可以获取到本机IP:IP4与IP6。这里,笔者使用QT获取本IP的方式如下:

头文件名: gethostIP_widget.h

头文件代码如下:

#ifndef GETHOSTIPWIDGET_H
#define GETHOSTIPWIDGET_H

#include <QLabel>
#include <QString>
#include <QHostInfo>
#include <QHostAddress>

class GetHostIP_Widget 
{
public:
	GetHostIP_Widget();
	QString getLocalIp();
};

#endif	//GETHOSTIPWIDGET_H

gethostIP_widget.cpp代码如下:

#include"gethostIP_widget.h"


GetHostIP_Widget::GetHostIP_Widget()
{

}


/*
	函数名:getLocalIp
	函数参数:无
	函数返回值: 返回QString类型的 IP
*/
QString GetHostIP_Widget::getLocalIp()
{
	QString vAddress;
#ifdef _WIN32

	QHostInfo vHostInfo = QHostInfo::fromName(QHostInfo::localHostName());
	QList<QHostAddress> vAddressList = vHostInfo.addresses();
#else
	QList<QHostAddress> vAddressList = QNetworkInterface::allAddresses();

#endif
	for(int i = 0; i < vAddressList.size(); i++)
	{

		if (!vAddressList.at(i).isNull() &&

			vAddressList.at(i) != QHostAddress::LocalHost &&

			vAddressList.at(i).protocol() ==  QAbstractSocket::IPv4Protocol)
		{
			vAddress = vAddressList.at(i).toString();

			//---检查显示的获取到的IP是否正确
			QLabel *b = new QLabel();
			b->setText("host IP : " + vAddress);
			b->show();

			break;//QT获取本机IP地址

		}
	}
	return vAddress;	
}
这样,用类GetHostIP_Widget 实例化一个对象,调用成员函数getLocalIp就能得到本机的IP4的地址。

若要获取本机IP46的地址, 只需要将:

QAbstractSocket::IPv4Protocol

代码改成:

QAbstractSocket::IPv6Protocol

就能获取本机的IP6的地址了




附,调用Windows的库函数获取本机IP4的地址方法如下:

1.首先包含头文件: Winsock2.h

2.附上下面的代码即可:

hostent *pHostent = gethostbyname("");
QString temStr( inet_ntoa(*(IN_ADDR*)pHostent->h_addr_list[0]) );
这样,就能得到一个QString的IP4的地址了。

hostent的说明如下:

The hostent structure is used by functions to store information about a given host, such as host name, IPv4 address, and so forth. An application should never attempt to modify this structure or to free any of its components. Furthermore, only one copy of the hostent structure is allocated per thread, and an application should therefore copy any information that it needs before issuing any other Windows Sockets API calls.

typedef struct hostent {
  char FAR* h_name;
  char FAR  FAR** h_aliases;
  short h_addrtype;
  short h_length;
  char FAR  FAR** h_addr_list;
} HOSTENT, 
 *PHOSTENT, 
 FAR *LPHOSTENT;

Members

h_name

The official name of the host (PC). If using the DNS or similar resolution system, it is the Fully Qualified Domain Name (FQDN) that caused the server to return a reply. If using a local hosts file, it is the first entry after the IP address.

h_aliases

A NULL-terminated array of alternate names.

h_addrtype

The type of address being returned.

h_length

The length, in bytes, of each address.

h_addr_list

A NULL-terminated list of addresses for the host. Addresses are returned in network byte order. The macro h_addr is defined to be h_addr_list[0] for compatibility with older software.


下面是hostent 的官方demo的代码:

//----------------------
// Declare and initialize variables
struct hostent* remoteHost;
char* host_addr;
unsigned int addr;

//----------------------
// User inputs IPv4 address of host
printf("Input IPv4 address of host: ");
host_addr = (char*) malloc(sizeof(char*)*16);
fgets(host_name, 16, stdin);

// If the user input is an alpha name for the host, use gethostbyname()
// If not, get host by addr (assume IPv4)
if (!isalpha(host_addr[0]))    /* host address is an IPv4 address */
{
  if (strlen(host_addr) < 16)
  {
     addr = inet_addr(host_addr);
     remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);
  }
  else
  {
     printf("error: Supplied host IPv4 address string is too large.\r\n");
     exit 255;
  }
}

if (WSAGetLastError() != 0)
{
  if (WSAGetLastError() == 11001)
  printf("Host not found...\nExiting.\n");
}
else
  printf("error#:%ld\n", WSAGetLastError());

// The remoteHost structure can now be used to
// access information about the host

你可能感兴趣的:(Qt学习之路之获取本机IP)