Unsolved QTcpServer / QTcpSocket help
-
@jsulm I can see incomingConnection is called as I have a breakpoint in the function on the last line.
-
@KroMignon Thank you, good call!
-
@SPlatten And second, where did you connect
QTcpServer::newConnection
?I am missing something like:
clsSocketServer::clsSocketServer(QObject* pParent) : QTcpServer(pParent) { connect(this, &QTcpServer::newConnection, [this](){ while(hasPendingConnections()) { auto socket = m_server->nextPendingConnection(); if(!socket) break; // do something with the client! } }); bool success = listen(QHostAddress::Any, clsSocketServer::mscuint16port); Q_ASSERT_X(!success,"clsSocketServer::clsSocketServer", strListenFailure.toLatin1().data()); }
-
@KroMignon, I used:
https://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.htmlAs a guide for the server, I don't see anything like that on that page?
-
@KroMignon said in QTcpServer / QTcpSocket help:
And second, where did you connect QTcpServer::newConnection?
He's overwriting QTcpServer::incomingConnection() because he follows the threading example (for unknown reasons)
-
@KroMignon, Are you sure your test in Q_ASSERT_X is correct? I get an exception immediately when using !isListening() or !success.
-
@Christian-Ehrlicher , is it wrong?
-
@SPlatten said in QTcpServer / QTcpSocket help:
Are you sure your test in Q_ASSERT_X is correct?
my bad, sorry: the test is inverted!
should beQ_ASSERT_X(success,"clsSocketServer::clsSocketServer", strListenFailure.toLatin1().data()); or Q_ASSERT_X(isListening(),"clsSocketServer::clsSocketServer", strListenFailure.toLatin1().data());
-
@KroMignon , Thanks for confirming.
-
@SPlatten said in QTcpServer / QTcpSocket help:
is it wrong?
It is not specially wrong, but using
QTcpServer::waitForConnected()
is a blocking method, so no event will be handled during this call for the used thread!Using
QTcpServer::newConnection()
signal is the non blocking alternative, which is more QEventLoop/QEvent friendly ;)==> Read documentation for more details
-
@KroMignon, I call connectToHost in the process that is launched this connects to the application that launched it and this uses QTcpSocket not QTcpServer. The new connection would be created by the launcher when it processes the connection.
I call waitForConnection purposely as the child process has no other purpose without a connection to the launcher.
I think the problem is in the launcher in this function:
void clsSocketServer::incomingConnection(qintptr sfd) { QTcpSocket* pSckClient = new QTcpSocket(this); connect(pSckClient, SIGNAL(readyRead()), this, SLOT(readClient())); connect(pSckClient, SIGNAL(disconnected()), this, SLOT(discardClient())); pSckClient->setSocketDescriptor(sfd); pSckClient->write("Hello", 5); }
I can see in the debugger an incomingConnection is called, but the test write, doesn't happen.
-
@KroMignon said in QTcpServer / QTcpSocket help:
Please change this into:
bool success = listen(QHostAddress::Any, clsSocketServer::mscuint16port);
Q_ASSERT_X(!success,"clsSocketServer::clsSocketServer", strListenFailure.toLatin1().data());or
listen(QHostAddress::Any, clsSocketServer::mscuint16port);
Q_ASSERT_X(!isListening(),"clsSocketServer::clsSocketServer", strListenFailure.toLatin1().data());You can use
Q_ASSUME
is for this kind of cases.Q_ASSUME(listen(QHostAddress::Any, clsSocketServer::mscuint16port));
@SPlatten said in QTcpServer / QTcpSocket help:
I used:
https://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.htmlUnfortunately that's not a good example to start on TCP. Some of us here prepared a better example to serve as a starting point, have a look at https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application
-
@VRonin , thank you, I will take a look now.
-
This post is deleted!