Extending networking/socket programming functionality of a qml project
-
Do you realize that QNetworkAccessManager is asynchronous ?
-
wrote on 3 Jun 2022, 19:50 last edited by qcoderpro 6 Mar 2022, 19:51
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? :(
wrote on 4 Jun 2022, 04:45 last edited by@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!"; }
-
You are using your code sequentially and not asynchronously. First understand that.
Use properly signals and slots.
You are also not doing any error checks server side.
It's also not even clear how your are reading and writing to your sockets now.
-
You are using your code sequentially and not asynchronously. First understand that.
Use properly signals and slots.
You are also not doing any error checks server side.
It's also not even clear how your are reading and writing to your sockets now.
wrote on 4 Jun 2022, 20:12 last edited byYou are using your code sequentially and not asynchronously.
What do you mean by that? I don't get it!
Use properly signals and slots.
You are also not doing any error checks server side.This is part of server's code embracing connections as well as error checks:
Server::Server(QObject *parent) : QObject{parent} , tcpSocket(new QTcpSocket(this)) , tcpServer(new QTcpServer(this)) { initServer(); } 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!"; } 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); } void Server::displayError(QAbstractSocket::SocketError socketError) { switch (socketError) { case QAbstractSocket::RemoteHostClosedError: break; case QAbstractSocket::HostNotFoundError: qInfo() <<"The host was not found. Please check the " "host name and port settings."; break; case QAbstractSocket::ConnectionRefusedError: qInfo() << "The connection was refused by the peer. " "Make sure the server is running, " "and check that the host name and port " "settings are correct."; break; default: qInfo() << "The following error occurred: " + tcpSocket->errorString(); } }
What other connections should I add?
It's also not even clear how your are reading and writing to your sockets now.
It's identical to the way both apps are running on the same machine.
It is writeMessage():void Server::writeMessage() { inOut.startTransaction(); QString message; inOut >> message; if (!inOut.commitTransaction()) setMessage("commitTransaction error"); else setMessage(message); } void Server::setMessage(const QString& newMessage) { message += newMessage + '\n'; emit messageChanged(message); }
-
What is inOut supposed to be ?
-
wrote on 7 Jun 2022, 07:51 last edited by
@SGaist
This is server's whole header:#ifndef SERVER_H #define SERVER_H #include <QObject> #include <QAbstractSocket> class QTcpServer; class QTcpSocket; class QNetworkReply; class Server : public QObject { Q_OBJECT Q_PROPERTY(QString message READ getMessage WRITE setMessage NOTIFY messageChanged) public: explicit Server(QObject *parent = nullptr); void setMessage(const QString&); QString getMessage() const; public slots: void initServer(); void onNewConnection(); void displayError(QAbstractSocket::SocketError); void sendMessage(const QString&); void writeMessage(); signals: void messageChanged(QString); private: QTcpSocket* tcpSocket { nullptr }; QTcpServer* tcpServer { nullptr }; QDataStream inOut; }; #endif // SERVER_H
And sendMessage's definition:
void Server::sendMessage(const QString& newMessage) { QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); inOut << newMessage; tcpSocket->write(block); }
-
So it looks like inOut is used uninitialized.
-
wrote on 7 Jun 2022, 19:37 last edited by qcoderpro 6 Jul 2022, 19:52
It's been initialized this way in
void Server::onNewConnection()
, that is, as soon as a new connection arrives:inOut.setDevice(tcpSocket); inOut.setVersion(QDataStream::QDataStream::Qt_5_10);
PS: The projects, using inOut the way above, work completely as expected when run on the same machine!
-
You should setup wireshark and check what happens exactly.
At some point you wrote you had a timeout error, that's one of the thing you should resolve first it means that one side fails to connect to the other.
Don't forget one important thing: your server needs to be accessible from outside of your network especially if you connect to it from a mobile device. You will have lots of items between your server and client like firewalls, NAT layers and whatnot.
81/92