Extending networking/socket programming functionality of a qml project
-
You are mixing noip API server and yours. They are not the same and the port you use for your application is unrelated to the one you use to update your account.
As for your socket issue, I do not see any error checking done in your code so you do not even know whether your connection was successful.
You are mixing noip API server and yours. They are not the same and the port you use for your application is unrelated to the one you use to update your account.
Sounds reasonable. So what port number should I use for my client app to be able to connect to the server app, please? The sever sends its IP by updating the record, but how about a port? How to find a port for the connection?
-
You are mixing noip API server and yours. They are not the same and the port you use for your application is unrelated to the one you use to update your account.
Sounds reasonable. So what port number should I use for my client app to be able to connect to the server app, please? The sever sends its IP by updating the record, but how about a port? How to find a port for the connection?
@qcoderpro said in Extending networking/socket programming functionality of a qml project:
So what port number should I use for my client app to be able to connect to the server app, please?
The port your server is listening for connections.
It is your server, so you should know what server you use. -
@qcoderpro said in Extending networking/socket programming functionality of a qml project:
So what port number should I use for my client app to be able to connect to the server app, please?
The port your server is listening for connections.
It is your server, so you should know what server you use.@jsulm
The server may be run by someone in another country, so how the client should know the port number!? :|
Furthermore, you said: port number can be fixed (like 8080 for HTTP).For the connection both IP and port are needed, As far as I'm concerned. No-ip works out the problem with IP. But how about the port number? :(
-
@jsulm
The server may be run by someone in another country, so how the client should know the port number!? :|
Furthermore, you said: port number can be fixed (like 8080 for HTTP).For the connection both IP and port are needed, As far as I'm concerned. No-ip works out the problem with IP. But how about the port number? :(
@qcoderpro Question: do you have to use a fix port number or not? Port can be fixed like it is for many protocols.
-
@qcoderpro Question: do you have to use a fix port number or not? Port can be fixed like it is for many protocols.
@jsulm
I don't know since I'm new to networking. Actually I chose Qt to learn socket programming. When running the apps on the same machine, the client needs both IP and port to connect to the server. The server publishes both, say, this for the port:QString::number(tcpServer->serverPort())
, and since I can see them, I enter IP and port in client's fields to connect to server.
Now that the apps are going to work remotely, for example, each running by a person in a different location (not on the same machine), the IP problem is going to be fixed using noip. But the port problem remains. So I assume there're two ways to work it out, 1- use a fixed port or 2- use any port the server offers but this way we need to somehow deliver that number to client to give it the ability to establish the connection.
Now you choose what to choose please. -
That's your application so that decision is yours.
As you were already explained, peer to peer communication has different constraints than the use of a central server.
-
That's your application so that decision is yours.
As you were already explained, peer to peer communication has different constraints than the use of a central server.
-
@SGaist
Since I've not been dealt with projects like that, I'm seeking the easiest way. An easy free service like No-ip. I wish it could solve the issue with the port as well. :|
What do you suggest, please?@qcoderpro said in Extending networking/socket programming functionality of a qml project:
What do you suggest, please?
I suggest to use a fixed port number...
-
@qcoderpro said in Extending networking/socket programming functionality of a qml project:
What do you suggest, please?
I suggest to use a fixed port number...
-
-
@SGaist
Sorry for the delay. I was very busy. If you think it's better, I'm going to create a new thread for the problem.I checked the page for the ports numbers and tested a couple of them which are related to messaging based on TCP.
My
server.cpp
has:Server::Server(QObject *parent) : QObject{parent} , tcpSocket(new QTcpSocket(this)) , tcpServer(new QTcpServer(this)) { initServer(); } QString Server::initServer() { tcpServer = new QTcpServer(this); if(!tcpServer->listen()) return "Server Unable to start the server: " + tcpServer->errorString(); connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection); QByteArray auth = "myEmail@gmail.com: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!"; } void Server::onNewConnection() { qInfo() << "New connection arrived!"; tcpSocket = tcpServer->nextPendingConnection(); connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &Server::displayError); inOut.setDevice(tcpSocket); inOut.setVersion(QDataStream::QDataStream::Qt_5_10); connect(tcpSocket, &QAbstractSocket::readyRead, this, &Server::writeMessage); } ...
First ran this app and got the message: Reply got back with no error!
client.cpp
contains:Client::Client(QObject *parent) : QObject{parent} , tcpSocket(new QTcpSocket(this)) { inOut.setDevice(tcpSocket); inOut.setVersion(QDataStream::Qt_5_10); connect(tcpSocket, &QAbstractSocket::readyRead, this, &Client::writeMessage); connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &Client::displayError); QHostInfo info = QHostInfo::fromName("coderdev.ddns.net"); sendAddress(info.addresses().front(), 4662); /* 18 Message Send Protocol[19][20] 157 Assigned KNET/VM Command/Message Protocol 218 Message posting protocol (MPP) 993 Internet Message Access Protocol over TLS/SSL (IMAPS)[11] 2123 GTP control messages (GTP-C) 2152 GTP user data messages (GTP-U) 2775 Short Message Peer-to-Peer (SMPP)[citation need 4662 OrbitNet Message Service */ } void Client::sendAddress(QHostAddress ip, unsigned int port) { tcpSocket->abort(); tcpSocket->connectToHost(ip, port); } ...
After running the server (and hoping that the record on noip has been updated correctly), ran the client and tested all ports above!
Yet no new connection nor an error from the displayError slot.
But Qt Creator gives the error: Cannot retrieve debugging output. for all of the ports!Where is the problem, please? :(
-
You do not set the port on the server.
Also, you create the QTcpServer object twice. Not an issue per se but it does not make any sense.
-
You do not set the port on the server.
Also, you create the QTcpServer object twice. Not an issue per se but it does not make any sense.
-
It's all in the documentation of the listen method.
-
It's all in the documentation of the listen method.
I firstly run the server and get: Reply got back with no error!, for this code:
void Server::initServer() { if(!tcpServer->listen(QHostAddress::Any, 4662)) qInfo() << "Server Unable to start the server: " + tcpServer->errorString(); connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection); QByteArray auth = "myEmail@gmail.com: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!"; }
Then when running the client and texting, nothing is shown on the other side and get the error: QIODevice::write (QTcpSocket): device not open, on the server. :|
-
I firstly run the server and get: Reply got back with no error!, for this code:
void Server::initServer() { if(!tcpServer->listen(QHostAddress::Any, 4662)) qInfo() << "Server Unable to start the server: " + tcpServer->errorString(); connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection); QByteArray auth = "myEmail@gmail.com: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!"; }
Then when running the client and texting, nothing is shown on the other side and get the error: QIODevice::write (QTcpSocket): device not open, on the server. :|
-
Do you realize that QNetworkAccessManager is asynchronous ?
-
There're many differences between synchronous and asynchronous data transmission. But despite many disadvantages asynchronous transmission involves it's still possible to use it for my two apps, otherwise you wouldn't suggest QNetworkAccessManager for that, I'm sure.
Do you mean that the data I send from the server to client or vice versa will be corrupted or dropped when traversing the routes? In asynchronous transmission, data is sent in form of byte or character. Five times I sent messages from client to the server and another five-time from server to the client. Waited for more than 15 minutes. Nothing, even one letter, arrived at the other side ! :(What I get instead was error messages from both projects.
Client:
"The following error occurred: Connection timed out" QNativeSocketEngine::write() was not called in QAbstractSocket::ConnectedState "The following error occurred: Connection timed out"
Server:
Reply got back with no error! QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open qt.qpa.mime: Retrying to obtain clipboard. qt.qpa.mime: Retrying to obtain clipboard.
What can I do? :(
-
There're many differences between synchronous and asynchronous data transmission. But despite many disadvantages asynchronous transmission involves it's still possible to use it for my two apps, otherwise you wouldn't suggest QNetworkAccessManager for that, I'm sure.
Do you mean that the data I send from the server to client or vice versa will be corrupted or dropped when traversing the routes? In asynchronous transmission, data is sent in form of byte or character. Five times I sent messages from client to the server and another five-time from server to the client. Waited for more than 15 minutes. Nothing, even one letter, arrived at the other side ! :(What I get instead was error messages from both projects.
Client:
"The following error occurred: Connection timed out" QNativeSocketEngine::write() was not called in QAbstractSocket::ConnectedState "The following error occurred: Connection timed out"
Server:
Reply got back with no error! QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open QIODevice::write (QTcpSocket): device not open qt.qpa.mime: Retrying to obtain clipboard. qt.qpa.mime: Retrying to obtain clipboard.
What can I do? :(
@SGaist
I think QNetworkAccessManager is merely responsible for sending the request (or as written in the codeget
ing the request) and since the reply brings no error so apparently there shouldn't be any more worries regarding that. The rest, however, is done by QTCPSoket/Server and other stuff which are connection oriented.QNetworkReply * reply = qnam->get(request); if(reply->error()) qInfo() << "ERROR!: " + reply->errorString(); else qInfo() << "Reply got back with no error!"; }