QTCP Failed to send data
Unsolved
Mobile and Embedded
-
@jsulm sorry,I removed the wait function yesterday, eliminating the recurring time delay caused by multiple calls, but the data is still not being sent successfully. Here is my code:```
Constructor
`SocketConnection::SocketConnection(QWidget *parent)
: QMainWindow(parent)
{server = new QTcpServer(this); connect(server, &QTcpServer::newConnection, this, &SocketConnection::newConnection); if (!server->listen(QHostAddress::Any, 6789)) { LOGE << "Error: Unable to start the server:" << server->errorString(); return; } LOGI << "Server started"; socket = new QTcpSocket(this); socket->setSocketOption(QAbstractSocket::LowDelayOption, 1); socket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, 64 * 1024); socket->setSocketOption(QAbstractSocket::SendBufferSizeSocketOption, 64 * 1024);
}``
senddata `void SocketConnection::sendData(const QByteArray &data) { if (socket->state() == QTcpSocket::ConnectedState) { int bytesWritten = socket->write(data); bool isflush=socket->flush(); LOGI << "send data111: " << bytesWritten; LOGI << "isflush: " << isflush; } else { LOGI << "Socket not connected, attempting to connect..."; socket->abort(); socket->deleteLater(); QTcpSocket *newsocket = new QTcpSocket(); newsocket->connectToHost("127.0.0.1", 6789); LOGI << "Reconnected successfully."; QThread::msleep(400); if (newsocket->state() == QTcpSocket::ConnectedState) { int bytesWritten = newsocket->write(data); bool isflush=socket->flush(); LOGI << "2isflush: " << isflush; LOGI << "22222send data after reconnect: " << bytesWritten; } } }``
call senddata
`void SocketConnection::PendingSendtwo()
{QString hexString = ""; ..... ..... ..... ..... QByteArray dataToSend = hexString.toUtf8(); sendData(dataToSend);
}`