Not able to send data via tcp client from thread
-
wrote on 9 Aug 2024, 06:29 last edited by
Hi,
I'm not able to send the data via tcp client from a thread I created in QT. I'm able to make a connection with the server via socket->connectToHost(QHostAddress("192.168.10.3"), 50007) but I'm not able to send or receive data with socket->write(QByteArray("Hello from QT\r\n")) and socket.read(64). The weird thing i observed is I'm able to send and receive data from the main function but I'm not able to debug why these functions are not working in a thread.
Can someone tell me how to fix this issue?
-
Hi,
I'm not able to send the data via tcp client from a thread I created in QT. I'm able to make a connection with the server via socket->connectToHost(QHostAddress("192.168.10.3"), 50007) but I'm not able to send or receive data with socket->write(QByteArray("Hello from QT\r\n")) and socket.read(64). The weird thing i observed is I'm able to send and receive data from the main function but I'm not able to debug why these functions are not working in a thread.
Can someone tell me how to fix this issue?
wrote on 9 Aug 2024, 07:00 last edited by ChrisW67 8 Sept 2024, 07:05Welcome to the Qt forums.
The weird thing i observed is I'm able to send and receive data from the main function but I'm not able to debug why these functions are not working in a thread.
Its likely you do not have a suitable event loop in the thread or the networking elements are not owned by the thread. In all likelihood you do not need threading at all.
I think you will have to post some code and describe what "not able to send or receive" really means or we will just be guessing what you have actually done.
-
wrote on 9 Aug 2024, 10:53 last edited by rohan136 8 Sept 2024, 10:59
This is my thread function. I've added QObject class while creating this class
void TCP_Thread::TCP_Run() { QByteArray Msg; socket = new QTcpSocket(this); socket->connectToHost(QHostAddress("192.168.10.3"), 50007); for(;;) { Msg = socket->read(64); qInfo() << Msg; socket->write(QByteArray("Hello from QT\r\n")); QThread::currentThread()->msleep(1000); } }
This is how I'm creating this thread in main:
TCP_Thread *TCP = new TCP_Thread(); QThread TCPThread; TCPThread.setObjectName("TCP Thread"); TCP->moveToThread(&TCPThread); QObject::connect(&TCPThread, &QThread::started, TCP, &TCP_Thread::TCP_Run ); TCPThread.start();
connectToHost API seems to be working as I'm able to connect to the IP address and port number but not able to transmit and receive any data. I've included QTcpSocket and QtNetwork in the .h file.
-
wrote on 9 Aug 2024, 14:20 last edited by JonB 8 Sept 2024, 14:22
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. -
And as (nearly) always when using threads for io operations in Qt - no need for a thread here at all...
-
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.
wrote on 10 Aug 2024, 08:03 last edited by JonB 8 Oct 2024, 08:11@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.
-
wrote on 27 Aug 2024, 10:15 last edited by
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?
-
wrote on 28 Aug 2024, 05:56 last edited by
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.
-
wrote on 28 Aug 2024, 06:52 last edited by
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.
wrote on 28 Aug 2024, 08:11 last edited by@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".
-
wrote on 29 Aug 2024, 07:19 last edited by
Hi Jon,
Thanks for the reply, I figured our was creating socket twice in my code that's why it was working properly. Now my issue is resolved.
-
Then please mark the topic as solved.