是否有跨平台方式使用Qt获取计算机的本地IP地址(即看起来像192.168.1.49
的东西)?
我想为Symbian手机制作FTP服务器,我想显示FTP客户端应该连接的IP地址。
答案 0 :(得分:40)
使用QNetworkInterface::allAddresses()
const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost);
for (const QHostAddress &address: QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost)
qDebug() << address.toString();
}
答案 1 :(得分:15)
QNetworkInterface::allAddresses()
会为您提供网络地址。然后,您可以将结果过滤为非回送地址的IPv4地址:
QList<QHostAddress> list = QNetworkInterface::allAddresses();
for(int nIter=0; nIter<list.count(); nIter++)
{
if(!list[nIter].isLoopback())
if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol )
qDebug() << list[nIter].toString();
}
答案 2 :(得分:5)
如果您需要的信息不仅仅是IP地址(如子网),您必须遍历所有接口。
QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
QNetworkInterface eth;
foreach(eth, allInterfaces) {
QList<QNetworkAddressEntry> allEntries = eth.addressEntries();
QNetworkAddressEntry entry;
foreach (entry, allEntries) {
qDebug() << entry.ip().toString() << "/" << entry.netmask().toString();
}
}
答案 3 :(得分:2)
QNetworkInterface返回大量地址。你必须过滤它们才能获得理想的结果:
foreach (const QNetworkInterface &netInterface, QNetworkInterface::allInterfaces()) {
QNetworkInterface::InterfaceFlags flags = netInterface.flags();
if( (bool)(flags & QNetworkInterface::IsRunning) && !(bool)(flags & QNetworkInterface::IsLoopBack)){
foreach (const QNetworkAddressEntry &address, netInterface.addressEntries()) {
if(address.ip().protocol() == QAbstractSocket::IPv4Protocol)
qDebug() << address.ip().toString();
}
}
}
答案 4 :(得分:2)
这是我实现的代码:本地主机的名称,IP,网络掩码和mac地址。
QString localhostname = QHostInfo::localHostName();
QString localhostIP;
QList<QHostAddress> hostList = QHostInfo::fromName(localhostname).addresses();
foreach (const QHostAddress& address, hostList) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address.isLoopback() == false) {
localhostIP = address.toString();
}
}
QString localMacAddress;
QString localNetmask;
foreach (const QNetworkInterface& networkInterface, QNetworkInterface::allInterfaces()) {
foreach (const QNetworkAddressEntry& entry, networkInterface.addressEntries()) {
if (entry.ip().toString() == localhostIP) {
localMacAddress = networkInterface.hardwareAddress();
localNetmask = entry.netmask().toString();
break;
}
}
}
qDebug() << "Localhost name: " << localhostname;
qDebug() << "IP = " << localhostIP;
qDebug() << "MAC = " << localMacAddress;
qDebug() << "Netmask = " << localNetmask;
答案 5 :(得分:1)
我想获得目标计算机的$(document).ready(function() {
**//I create dataTable 1**
var table = $('#example').DataTable({
"language": {
"url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Spanish.json"
}, "dom": '<"toolbar">frtip'
});
**//I create dataTable 2**
var table2 = $('#example2').DataTable({
"language": {
"url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Spanish.json"
}, "dom": '<"toolbar">frtip'
});
**//I load dataTable 1**
$('#example tbody').on( 'click', 'tr', function () {
if ( $(this).hasClass('selected') ) {
$(this).removeClass('selected');
}else{
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
var getted = table.row('.selected').data();
$.ajax({
type:'POST',
data: 'usuario=' + getted,
url: 'loadF1.php',
success: function(msg) {
$('#example2 tbody').html(msg); //Here set content into div2
}
});
**//I load dataTable 2**
$('#example2 tbody').on( 'click', 'tr', function () {
if ( $(this).hasClass('selected') ) {
$(this).removeClass('selected');
}else{
table2.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
var getted2 = table2.row('.selected').data();
$.ajax({
type:'POST',
data: 'usuario=' + getted2,
url: 'loadF2.php',
success: function(msg) {
$('#example3 tbody').html(msg); //Here set content into div3
}
});
IP地址。上面提供的答案帮助我得到了我想要的东西:这就是我编写函数以获取网络接口名称eth1
的IP地址的方式。
eth1