Find public IP of my internet connection
-
Hi to all. Finally i'm opening a new Thread to figure out this problem. I've need to know from my qt code, the public ip iddress of my internet connection.
I'm using qt (Qt Creator 3.2.1 Based on Qt 5.3.2 (GCC 4.9.2, 32 bit)) on my raspberry and i've already try some solution but unfortunately without success.
This is the code that i tested, SIGILL signals from the OS coming up...please help me to find a workaround to figure out this problem.Widget::Widget(QWidget *parent) :QWidget(parent), ui(new Ui::Widget) { QEventLoop eventLoop; QNetworkAccessManager *netManager = new QNetworkAccessManager(this); QUrl url("https://www.whatismyip.org/"); QNetworkRequest req(url); QNetworkReply *reply = netManager->get(req); //this causes error SIGILL connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit())); eventLoop.exec(); //other instructions// }
Same problem that i found here : qt forum same problem click
-
@markolino_it
Given the behaviour, I thought the workarounds mentioned from the link reference you gave are what is available for RPi? Like they ended up pointing to:Based on the thread, I'm assuming the SIGILL is intentional by SSL and harmless since it's trapped somehow.
? Obvious question is whether you get the
SIGILL
when not running under debugger? And second question is do you not get it when not accessing anhttps
address? -
Don't call QEventLoop, esp. not in ctor of a class but use proper signal/slots instead.
-
@JonB said in Find public IP of my internet connection:
Given the behaviour, I thought the workarounds mentioned from the link reference you gave are what is available for RPi? Like they ended up pointing to:
Based on the thread, I'm assuming the SIGILL is intentional by SSL and harmless since it's trapped somehow.
? Obvious question is whether you get the SIGILL when not running under debugger? And second question is do you not get it when not accessing an https address?
Thanks for the answer. It is the first time that i use these function to get a reply from the network, so i only used it with under the debugger.
Out of the debugger no errors coming up with the same code.
But i can not test the code. How i can print the reply on the qDebug() console for example ? How i can access the ip in the reply ?
Thanks a lot. -
@Christian-Ehrlicher
Thanks a lot. -
@markolino_it
most probably the SIGILL signal is coming from the OpenSSL libs upon initialization.
you should be able to simply jump over (F5) this signal and continue with your application. -
@markolino_it
If you followed the link you quoted for the same problem, one suggestion at https://forums.raspberrypi.com/viewtopic.php?t=59328&p=814919 isUnder Tools->Options->Debugger->gdb, Add this in the "Additional Startup Commands" entry box:
handle SIGILL noprint nostop
This "fixes" the problem, but ALL SIGILLS will be ignored. Hopefully those that are "real issues" cause something else to happen a few instructions later that's not hard to isolate. Or, just hope you don't have any "real ones" :lol:
-
I'm tryng to test this code without the debugger but unfortunately i can't see any "RESPONSE" printed in the console.
QNetworkAccessManager *netManager = new QNetworkAccessManager(this); QUrl url("https://www.whatismyip.org/"); QNetworkRequest req(url); QNetworkReply *reply = netManager->get(req); connect(reply, &QNetworkReply::finished, [=]() { if(reply->error() == QNetworkReply::NoError) { QByteArray response = reply->readAll(); qDebug()<< "RESPONSE#########"; //it never comes here // do something with the data... } else // handle error { qDebug()<<reply->errorString(); } }); reply = netManager->get(req);
-
@markolino_it
Put aqDebug()
above thereply->readAll()
, and more generally put one in as the first statement in the slot-lambda, obviously we first want to know whether it's even getting there.Do you really have two
netManager->get(req)
as you show? Because the second one overwrites thereply
object returned from the first one. Please always post actual code. -
Finally got it works. I Share my solution.
I create a new QWidget Application with a pushButton, so in widget.h :public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void on_pushButton_clicked(); void on_readData(); private: Ui::Widget *ui; QNetworkAccessManager *netManager; QNetworkReply *reply;
public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void on_pushButton_clicked(); void on_readData(); private: Ui::Widget *ui; QNetworkAccessManager *netManager; QNetworkReply *reply;
and in widget.cpp
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); netManager = new QNetworkAccessManager(this); } Widget::~Widget() { delete ui; } void Widget::on_pushButton_clicked() { reply = netManager->get(QNetworkRequest(QUrl("https://www.whatismyip.org/"))); connect(reply, SIGNAL(finished()), this, SLOT(on_readData())); } void Widget::on_readData() { if(reply->error() == QNetworkReply::NoError) { QByteArray response = reply->readAll(); //All the HTML code (include IP ADDRESS) qDebug()<< response; } else // handle error { qDebug()<<reply->errorString(); } }
More info at : QtDocumentation
I hope that this can help other people !
Now i think that i can search for "IP" in the ByteArray and store them in a QString or things like that...
Thanks a lot. -
@markolino_it Then please mark the topic as solved.