Not able to send data via tcp client from thread
-
Shouldn't you wait till you have received QAbstractSocket::connected() before starting to read/write? You don't even know whether it has connected for sure.
You should put some error handling in code if you are having problems, e.g. on reads and writes.
I trust yourQThread TCPThread;
stays in scope.
Your code does not allow the Qt event loop to run (in the thread). I don't know how that plays with the socket connection/read/write code. It may be fine, I just don't know. -
ConnectToHost API works as I get the log messages in the server that the connection is established.
@rohan136
I don't see why you would not check that is reflected in the client no matter what happens at the server side. Theoretically at least it might connect at the server side but fail to recognise this or go wrong at the client side.Do you not think your client side code should wait for the connection established/finished signal before it starts reading/writing the socket? Even if you know you see the connection established at the server side you do not know when that is seen at the client side.
connectToHost()
is asynchronous, the socket is not necessarily connected when you execute the next line in the caller. And I don't know howread()
behaves on an as-yet-not-connected socket.In the code you show so far at least as @Christian-Ehrlicher says there is no need to use any separate thread. You may need one for some purposes, but "the majority" of cases like this we see, at least from new Qt users, have no need of threads.
-
Hi,
I followed your advice and used signal and slots method for tcp without thread. But right now, I'm facing another issue.
I've created a C++ class in qml project. And on button click, I'm calling the below function:void TCP_Backend::startTcpNetwork()
{TCP_Backend TCP_Class; MySocket = new QTcpSocket; QObject::connect(MySocket, &QTcpSocket::connected, this, &TCP_Backend::IncomingConnection); QObject::connect(MySocket, &QTcpSocket::readyRead, this, &TCP_Backend::ReadMessage); QObject::connect(MySocket, &QTcpSocket::disconnected,this, &TCP_Backend::OnDisconnection); QObject::connect(this, &TCP_Backend::StartScreenMirroring, this, &TCP_Backend::SendScreenShot); QObject::connect(this, &TCP_Backend::ListenToClient, this, &TCP_Backend::WaitForConnection); WaitForConnection(); qInfo() << "Socket state" << MySocket->state();
}
I don't know why but receiver functions in the connect API are not called. When I connect my client with the server, IncomingConnection function should be called, but it's not being called. Also, when I'm checking directly with MySocket->state(), the return value of this function is ConnectedState. Can someone help me why receiver functions are not being called?
Note: startTcpNetwork is present in TCP_Backend Class.
-
You create a socket but don't connect to an endpoint - so what should happen?
-
What do you mean don't connect to an endpoint? WaitForConnection() has connectToHost and waitForConnected. I mentioned earlier the issue is not for communication, MySocket->state() returns ConnectedState. IncomingConnection API should be triggered by connected signal which is not happening.
-
I think should look at this example first: https://doc.qt.io/qt-6/qtnetwork-fortuneclient-example.html
-
Please provide all relevant code, not just some parts. Provide a minimal compileable example.
-
What do you mean don't connect to an endpoint? WaitForConnection() has connectToHost and waitForConnected. I mentioned earlier the issue is not for communication, MySocket->state() returns ConnectedState. IncomingConnection API should be triggered by connected signal which is not happening.
@rohan136
We don't know what yourWaitForConnection()
code might or might not do.Are we to guess
MySocket
is a member variable inTCP_Backend
?Why does your code have a local variable of
TCP_Backend TCP_Class;
and what does your code do with it? Why would a member methodTCP_Backend::startTcpNetwork()
create an instance ofTCP_Backend
under any circumstances? What is the lifetime of whateverTCP_Backend
instance that methodTCP_Backend::startTcpNetwork()
is being called on?This is why you need to provide some "minimal compileable example".
-
Then please mark the topic as solved.