TCP in Qt don't send data
-
Hello, good afternoon,
I have a problem with Qt. I have created a class that sends data through a TCP socket without depending on the Qt part. I have tested in a test that the data is sent every 1Hz without closing the communication and it has worked correctly. The problem is the following:
When I add this class to Qt and create a thread to send data every 1Hz. The server receives all the accumulated data when the socket is closed or the application is closed.
Do I have to configure something in qt to allow TCP data sending?Also, I have tried Qtcpsocket and it behaves the same.
Thank you so much for your help.
-
@Athena_S said in TCP in Qt don't send data:
Do I have to configure something in qt to allow TCP data sending?
Nope it should all work. Yes you should use
QTcpSocket
. Your code is probably not doing something correctly, we need to see it (or preferably a minimal example of an issue) to comment further.... -
@JonB said in TCP in Qt don't send data:
Your code is probably not doing something correctly
-
Constructor(std::string ip, uint32_t port, Socket::protocol protocol_type) { // This is a Socket that i created /*socket_server_= std::make_unique<Socket>(ip, port, protocol_type); socket_server_->init(false);*/ host_address_ = QHostAddress(QString::fromStdString(ip)); tcp_client_socket_.reset(new QTcpSocket()); tcp_client_socket_->connectToHost(QString::fromStdString(ip), port); } void sendDataFunction() { iris_data_.updateTime(); size_t size = iris_data_.telemetryData.getSumAllBytes(); char* command = new char[size]; uint8_t* msg = iris_data_.telemetryData.getMsg(); memcpy(command, msg, size_t(iris_data_.telemetryData.getSumAllBytes())); tcp_client_socket_->write(command,size); tcp_client_socket_->waitForBytesWritten(1000); delete[] command; }
I create this class, with his constructor and send data member function.
When the instance of the class is created, a qtimer call sendDataFunction every second.To read the data a use a TCP Server in python.
import socket import sys # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port server_address = ('localhost', 10000) print('starting up on {} port {}'.format(*server_address)) sock.bind(server_address) # Listen for incoming connections sock.listen(1) while True: # Wait for a connection print('waiting for a connection') connection, client_address = sock.accept() try: print('connection from', client_address) # Receive the data in small chunks and retransmit it while True: data = connection.recv(241) print('received {}'.format(data)) if data: print('sending data back to the client') #connection.sendall(data) else: print('no data from', client_address) break finally: # Clean up the connection connection.close()
QTIMER:
timer_pcs_iris_.reset(new QTimer(this)); timer_pcs_iris_.get()->setInterval(1000); connect(timer_pcs_iris_.get(), SIGNAL(timeout()), this, SLOT(sendData())); timer_pcs_iris_.get()->start(); void sendData() { //Instance of the classs pcs_iris_->sendDataFunction(); }
-
@Athena_S said in TCP in Qt don't send data:
I create this class, with his constructor and send data member function
You forgot to post the code where you're actually calling sendDataFunction().
Also, you do not check whether connectToHost succeeds, basically you do not have any error handling. -
@Athena_S Did you check whether sendData() is called?
And please add error handling!
For example: what does tcp_client_socket_->write(command,size) return?
Also connect a slot to https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred and print error there.
All this is something you should anyway do...