You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

kpmsiva

Beginner

  • "kpmsiva" is male
  • "kpmsiva" started this thread

Posts: 14

Location: Windows Vista

Occupation: Student

  • Send private message

1

Saturday, February 21st 2009, 5:18am

Ip address

Hi !

guys, In my project i need to find out the current system ip address. By using ifconfig we can get ip address. But i really need ipaddress by using the qt. I tried with QHostAddress, but leads failed (:. How can i get ip address of local mech using qt??? :(

2

Saturday, February 21st 2009, 7:24am

Try to use QNetworkInterface.
Fighting fire with fire.
Three can keep a secret if two of them are dead.

kpmsiva

Beginner

  • "kpmsiva" is male
  • "kpmsiva" started this thread

Posts: 14

Location: Windows Vista

Occupation: Student

  • Send private message

3

Saturday, February 21st 2009, 9:42am

thanks for your reply. I read the documentation , but still i didnt get.

I tried following....

Source code

1
2
3
4
QProcess *proc=new QProcess();
proc->start("ifconfig eth0 | grep ‘inet ‘ | awk ‘{print $2}’ | sed ’s/addr://’");
proc->waitForFinished(-1);
QString out=proc->readLine();


if i worked with the shell its worked fine. But by using the QProcess i am not getting??? Help me out

This post has been edited 1 times, last edit by "kpmsiva" (Feb 21st 2009, 9:53am)


4

Saturday, February 21st 2009, 9:48am

What is the problem? Please, post some code.

Source code

1
2
3
  QList<QHostAddress> hosts = QNetworkInterface::allAddresses ();
  for (int i = 0; i < hosts.count(); ++i)
       somewhere output or process hosts[i]. of course 127.0.0.1 can be ignored.
Fighting fire with fire.
Three can keep a secret if two of them are dead.

kpmsiva

Beginner

  • "kpmsiva" is male
  • "kpmsiva" started this thread

Posts: 14

Location: Windows Vista

Occupation: Student

  • Send private message

5

Saturday, February 21st 2009, 10:10am

I posted the sample code in the above post. I cann't use the grep and pipe symbol in QProcess in order to get ip address only as output. I tried your sample and getting error message as error :QNetworkInterface No such file or directory!. I think to include a line in .pro file which line to add to work?. My need is output of ipaddress thats it.

6

Saturday, February 21st 2009, 10:16am

Qt version? add
QT += network
in pro file (or through project manager). and rerun qmake make.
As for above your code. Try readAllStandardOutput after finish, try to execute
"sh ifconfig eth0 | grep 'inet ‘ | awk ‘{print $2}’ | sed ’s/addr://’"
not sure it will run. now on winxp.
Fighting fire with fire.
Three can keep a secret if two of them are dead.

kpmsiva

Beginner

  • "kpmsiva" is male
  • "kpmsiva" started this thread

Posts: 14

Location: Windows Vista

Occupation: Student

  • Send private message

7

Saturday, February 21st 2009, 10:21am

thanks for quick reply... I am using QT 4. Let me check out now....

kpmsiva

Beginner

  • "kpmsiva" is male
  • "kpmsiva" started this thread

Posts: 14

Location: Windows Vista

Occupation: Student

  • Send private message

8

Saturday, February 21st 2009, 5:42pm

Finally i got it the ipaddress only by using the QProcess.

My code was

Source code

1
2
3
4
5
6
7
8
QString prog = "/bin/bash";//shell
QStringList arguments;
arguments << "-c" << "ifconfig eth0 | grep ‘inet ‘ | awk ‘{print $2}’ | sed ’s/addr://’";
QProcess* process = new QProcess(this);
process->start(prog , arguments);
process->waitForFinished();
QString tmp = process->readAll();
qDebug() << tmp;


Above code will return only the ip address of the local mechine [ etho inet address: ]