PDA

View Full Version : how to read pc's network IP address



wei243
7th March 2007, 05:47
how use qt coding to read pc's network IP / internet IP address instead of 127.0.0.1?:confused:

patrik08
7th March 2007, 10:52
how use qt coding to read pc's network IP / internet IP address instead of 127.0.0.1?:confused:

On Window OS....




void Gui_FrontPage::TestQprocessqt4()
{

QProcess process;
#if defined Q_WS_WIN
process.setReadChannelMode(QProcess::MergedChannel s);
process.start("ipconfig" , QStringList() << "/all");

if (!process.waitForFinished()) {
msgb->information( this , "QT4 function test", process.errorString() );
} else {
msgb->information( this , "QT4 function test", process.readAll() );
}

#endif
}


on linux only admin user can discovery
sudo ifconfig

wei243
8th March 2007, 07:52
thx patrix08,
it is amazing - ur program show all the detail of ipconfig.
if i want to show ip address only, wat should i do?

patrik08
8th March 2007, 08:17
thx patrix08,
it is amazing - ur program show all the detail of ipconfig.
if i want to show ip address only, wat should i do?


first job .... split all line to a qstringlist....
register gateway or dns or network adress...
&& after .... http://www.regular-expressions.info/examples.html ...IP . && grep your
ip http://doc.trolltech.com/4.2/qregexp.html

similar to http://www.qtforum.de/forum/viewtopic.php?t=2114&highlight=regex

wei243
9th March 2007, 08:15
i failed to try the QRegExp in reading the ip address from qprocess's return string.
actually i m a newbie n never seen QRegExp.
pls give me an example coding to read it.
thx

patrik08
9th March 2007, 10:09
i failed to try the QRegExp in reading the ip address from qprocess's return string.
actually i m a newbie n never seen QRegExp.
pls give me an example coding to read it.
thx

Google have so match QRegExp ip adress
on is.....
http://lists.trolltech.com/qt-interest/2004-05/thread00085-0.html
search on http://www.koders.com/ you can find app piece and code trick ...


if you beginn on qt search qt function on http://www.koders.com/ && test it....

I learning qt 50% from http://www.koders.com/ && 30% from books and the rest from my php5 objekt know-how ....

My original work is cook chief 15 years a go.... and on 1999 i begin to write php & phython


untested code .....





QString incomming = "result from ipconfig /all"; /* qprocess read*/
QStringList alline = split("\n");;


for (int i = 0; i < alline.size(); ++i) {
QString onrow = alline.at(i);
if (onrow.contain("dns")) {
/* list onrow.split(" "); */
qDebug() << "### onrow " << onrow;
}

}

QString myip = "[0-9]{1,3}(.[0-9]{1,3}){3,3}";


QString str = "dgsdgsdgsdgsgsg 192.168.1.71 sdgdsgsgsgsdgsdgsg";

QRegExp expression( myip , Qt::CaseInsensitive );
expression.setMinimal(true);
int iPosition = 0;
while( (iPosition = expression.indexIn( str , iPosition )) != -1 ) {
QString grep = expression.cap( 1 );

qDebug() << "### grep " << grep;
iPosition += expression.matchedLength();
}



&& on line 84 from http://qt-webdav.svn.sourceforge.net/viewvc/qt-webdav/html_editor/lib_edit/html_loader.h?view=markup
you find a sample wo find html image tag....
from my projekt

wei243
10th March 2007, 06:13
i manage to get ip now. thx alot.

wysota
10th March 2007, 12:00
With QT 4.2+ you can use QNetworkInterface:

#include <QtDebug>
QList<QHostAddress> addrlist = QNetworkInterface::allAddresses();
foreach(QHostAddress addr, addrlist){
qDebug() << addr.toString();
}

wandson
5th January 2010, 14:06
Continuing this thread, how'd you do that on linux. Specifically, I need to retrieve gateway address on linux.
Regards,

wysota
5th January 2010, 14:31
The gateway address is not tied to an interface. Each system can have any number of gateways. You can run "route -n" and parse it to find the default gateway (one for 0.0.0.0 network) or do the same for /proc/net/route but that's probably everything that can be done (unless HAL exposes gateway definitions).

ManicQin
5th January 2010, 17:01
The gateway address is not tied to an interface. Each system can have any number of gateways. You can run "route -n" and parse it to find the default gateway (one for 0.0.0.0 network) or do the same for /proc/net/route but that's probably everything that can be done (unless HAL exposes gateway definitions).

Meaning there is no ... QT way to get the gateway?
Is there a Qt wrapper class for getting the Adapter information?
(information such as the DHCP enabled, adapter name and etc. )

wysota
5th January 2010, 17:36
Gateway and network interface are two completely different things. For the latter see QNetworkInterface.

wandson
8th January 2010, 16:59
Thanks for the answer. I guess the simplest way is to use QProcess to run route -n command.
Regards