QTCP Failed to send data
-
wrote on 15 Oct 2024, 13:01 last edited by
-
The data sending failed, and the data I wanted to send was received by socket->readAll(), and this socket->readAll() received it repeatedly many times.
it its my class
@spomm Please post code as text, not pictures.
We do not know where and how often you call sendData, so hard to say what is happening. -
@spomm Please post code as text, not pictures.
We do not know where and how often you call sendData, so hard to say what is happening.wrote on 16 Oct 2024, 01:31 last edited by@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);
}`
-
wrote on 16 Oct 2024, 01:49 last edited by
The original MInGw compiler has not found this problem, because of code reasons to change to the MSVC compiler, this problem occurs frequently.This problem is not frequent.
1/4