Extending networking/socket programming functionality of a qml project
-
No, the server has the address the client application is interested in. It does not get it from noip. noip is a DNS service that associates a hostname with an IP address, nothing more.
As for writing code, see their documentation about that matter.
@SGaist
Before writing the code, please let me understand the case completely.
The server has an IP address that the client is interested in (for establishing the connection). So how the client can know of that IP address?
The client can connect to hostname at noip and get the address of that hostname. But it's different from the server's IP! The server somehow should deliver its IP to the client for the connection. How is it done? -
The client just uses the hostname. That's all.
-
- So to sum it up: The client connects to my hostname which has been updated by the server to show its IP address and since this way the client can see the server's IP address it can connect to it. Right?
- If the above question is right, is this page the one I should study on how to make the server app update the hostname's record with its IP?
-
- Yes
- Looks like it yes.
-
So in my server app there should be a line of code as an HTTP request, like this example (of that page):
http://username:password@dynupdate.no-ip.com/nic/update?hostname=mytest.example.com&myip=192.0.2.25So if:
my username at noip is: coderdevqt@gmail.com
my password there is: passwordexample
and my hostname there is: coderdev.ddns.netthat HTTP request should be:
coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net
Right? -
Don't you think it would have been faster to just write and test the query ?
Also: https.
-
I'm not sure how to make an HTTP request using
QNetworkAccessManager
by a single string. I tired this but neither of theqInfo()
prints anything. The program also terminates at once! :|QNetworkAccessManager* manager = new QNetworkAccessManager(this); const QNetworkRequest request; const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"}; manager->put(request,data); connect(manager, &QNetworkAccessManager::finished, [] { QNetworkReply* reply = nullptr; if(reply->error()) qInfo() << "ERROR!: " + reply->errorString(); else qInfo() << "Reply got back with no error!"; reply->deleteLater(); });
-
I'm not sure how to make an HTTP request using
QNetworkAccessManager
by a single string. I tired this but neither of theqInfo()
prints anything. The program also terminates at once! :|QNetworkAccessManager* manager = new QNetworkAccessManager(this); const QNetworkRequest request; const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"}; manager->put(request,data); connect(manager, &QNetworkAccessManager::finished, [] { QNetworkReply* reply = nullptr; if(reply->error()) qInfo() << "ERROR!: " + reply->errorString(); else qInfo() << "Reply got back with no error!"; reply->deleteLater(); });
@qcoderpro said in Extending networking/socket programming functionality of a qml project:
connect(manager, &QNetworkAccessManager::finished, [] {
QNetworkReply* reply = nullptr;
if(reply->error()) -
I'm not sure how to make an HTTP request using
QNetworkAccessManager
by a single string. I tired this but neither of theqInfo()
prints anything. The program also terminates at once! :|QNetworkAccessManager* manager = new QNetworkAccessManager(this); const QNetworkRequest request; const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"}; manager->put(request,data); connect(manager, &QNetworkAccessManager::finished, [] { QNetworkReply* reply = nullptr; if(reply->error()) qInfo() << "ERROR!: " + reply->errorString(); else qInfo() << "Reply got back with no error!"; reply->deleteLater(); });
@qcoderpro
I would also suggest you do theconnect(manager, ...)
beforemanager->put()
not afterwards. -
@SGaist
What do you mean by copying part of the code I wrote above? I didn't get it. Do you mean those three lines of code are sufficient to make an Http request? Then how about the string!? :|@JonB
You mean this way?QNetworkAccessManager* manager = new QNetworkAccessManager(this); const QNetworkRequest request; const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"}; connect(manager, &QNetworkAccessManager::finished, [] { QNetworkReply* reply = nullptr; if(reply->error()) qInfo() << "ERROR!: " + reply->errorString(); else qInfo() << "Reply got back with no error!"; reply->deleteLater(); }); manager->put(request,data);
I'm confused! :(
Furthermore theput
function uploads the contents ofdata
to the destinationrequest
, while both my data and destination are packed into thatdata
! -
@SGaist
What do you mean by copying part of the code I wrote above? I didn't get it. Do you mean those three lines of code are sufficient to make an Http request? Then how about the string!? :|@JonB
You mean this way?QNetworkAccessManager* manager = new QNetworkAccessManager(this); const QNetworkRequest request; const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"}; connect(manager, &QNetworkAccessManager::finished, [] { QNetworkReply* reply = nullptr; if(reply->error()) qInfo() << "ERROR!: " + reply->errorString(); else qInfo() << "Reply got back with no error!"; reply->deleteLater(); }); manager->put(request,data);
I'm confused! :(
Furthermore theput
function uploads the contents ofdata
to the destinationrequest
, while both my data and destination are packed into thatdata
!@qcoderpro said in Extending networking/socket programming functionality of a qml project:
@JonB
You mean this way?Yes, I think it's preferable to attach the slot to
manager
's signal before you callmanager->put()
. Though @VRonin pointed out it doesn't actually matter in this case, but it's good style anyway. -
Since I've been summoned... how is this not a segfault?
QNetworkReply* reply = nullptr; if(reply->error())
What you should do is connect the
QNetworkReply
, not theQNetworkAccessManager
.// the initialisation of these 2 looks wrong but I'll ignore it for now QNetworkAccessManager* manager = new QNetworkAccessManager(this); const QNetworkRequest request; const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"}; QNetworkReply* reply = manager->put(request,data); connect(reply,&QNetworkReply::finished,this,[reply](){ if(reply->error()) qInfo() << "ERROR!: " + reply->errorString(); else qInfo() << "Reply got back with no error!"; }); connect(reply,&QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
-
Since I've been summoned... how is this not a segfault?
QNetworkReply* reply = nullptr; if(reply->error())
What you should do is connect the
QNetworkReply
, not theQNetworkAccessManager
.// the initialisation of these 2 looks wrong but I'll ignore it for now QNetworkAccessManager* manager = new QNetworkAccessManager(this); const QNetworkRequest request; const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"}; QNetworkReply* reply = manager->put(request,data); connect(reply,&QNetworkReply::finished,this,[reply](){ if(reply->error()) qInfo() << "ERROR!: " + reply->errorString(); else qInfo() << "Reply got back with no error!"; }); connect(reply,&QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
-
@qcoderpro said in Extending networking/socket programming functionality of a qml project:
I've used the exact same string as yours, of course with my real info.
Yeah, I expected something like that, hence my comment:
the initialisation of these 2 looks wrong but I'll ignore it for now
Your request has no url...
I have no idea how the server you are trying to connect to works so it's hard to tell you exactly what to do. Did you manage to do what you want with the server with another tool (like curl, or a browser-based rest client)? -
@qcoderpro said in Extending networking/socket programming functionality of a qml project:
const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"};
This is not the payload, it's the full query. The API has no payload.
Please read again the documentation.
-
@qcoderpro said in Extending networking/socket programming functionality of a qml project:
const QByteArray data {"https://coderdevqt@gmail.com:passwordexample@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"};
This is not the payload, it's the full query. The API has no payload.
Please read again the documentation.
@SGaist @VRonin
I'm rather confused.
Do you mean to split the string into two parts, one fordata
, and the other forrequest
like this?const QByteArray data {"https://coderdevqt@gmail.com:passwordexample"}; const QNetworkRequest request; request.rawHeader("@dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"); QNetworkAccessManager* manager = new QNetworkAccessManager(this); QNetworkReply* reply = manager->put(request,data); connect(reply,&QNetworkReply::finished,this,[reply](){ if(reply->error()) qInfo() << "ERROR!: " + reply->errorString(); else qInfo() << "Reply got back with no error!"; }); connect(reply,&QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
The result shows the same error! :|
-
Here is an example that shows how to do it in a simple fashion with the various elements of the query separated (replace what is found between <> with adequate values):
QByteArray auth = "<username>:<password>"; QByteArray authHeaderData = "Basic " + auth.toBase64(); QUrl requestUrl("https://dynupdate.no-ip.com/nic/update"); requestUrl.setQuery("hostname=<your-host-name>&myip=<your-ip-address>"); QNetworkRequest request(requestUrl); request.setRawHeader("Authorization", authHeaderData); QNetworkReply * reply = qnam.get(request);
-
Here is an example that shows how to do it in a simple fashion with the various elements of the query separated (replace what is found between <> with adequate values):
QByteArray auth = "<username>:<password>"; QByteArray authHeaderData = "Basic " + auth.toBase64(); QUrl requestUrl("https://dynupdate.no-ip.com/nic/update"); requestUrl.setQuery("hostname=<your-host-name>&myip=<your-ip-address>"); QNetworkRequest request(requestUrl); request.setRawHeader("Authorization", authHeaderData); QNetworkReply * reply = qnam.get(request);
@SGaist
The partmyip
is optional because the server's IP address connecting to their system will be used. So I use the version below, if you agree:QByteArray auth = "myEmail:myPass"; QByteArray authHeaderData = "Basic " + auth.toBase64(); QUrl requestUrl("https://dynupdate.no-ip.com/nic/update"); requestUrl.setQuery("hostname=coderdev.ddns.net"); QNetworkRequest request(requestUrl); request.setRawHeader("Authorization", authHeaderData); QNetworkReply * reply = qnam.get(request);
Two more questions:
1- We've dropped the @ sign existing in the first string. Doesn't matter?
2- What'sqnam
, please? -
- Sure it does matter, that's the authentication part. Re-read the code.
- a QNetwornAccessManager object.
-
- Sure it does matter, that's the authentication part. Re-read the code.
- a QNetwornAccessManager object.
@SGaist
I meant the @ in the string:
"...//coderdevqt@gmail.com:passwordexample@
dynupdate.no-ip.com/nic/update?hostname=coderdev.ddns.net"The message "Reply got back with no error!" is printed for this:
QByteArray auth = "myEmail:myPass"; QByteArray authHeaderData = "Basic " + auth.toBase64(); QUrl requestUrl("https://dynupdate.no-ip.com/nic/update"); requestUrl.setQuery("hostname=coderdev.ddns.net"); QNetworkRequest request(requestUrl); request.setRawHeader("Authorization", authHeaderData); QNetworkAccessManager* qnam = new QNetworkAccessManager(this); QNetworkReply * reply = qnam->get(request); if(reply->error()) qInfo() << "ERROR!: " + reply->errorString(); else qInfo() << "Reply got back with no error!";
So probably the record has been updated successfully.
For the client app I used the code below but the messages don't appear to be exchanged between the apps!
QHostInfo info = QHostInfo::fromName("coderdev.ddns.net"); // If updating over HTTPS, our system listens on Port 443 sendAddress(info.addresses().front(), 443); // ... void Client::sendAddress(QHostAddress ip, unsigned int port) { tcpSocket->abort(); tcpSocket->connectToHost(ip, port); }