Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QTCP Failed to send data

QTCP Failed to send data

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 2 Posters 267 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spomm
    wrote on last edited by
    #1

    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
    4033cb12-499e-49c6-a092-0f6fb8ab5ce6-image.png

    jsulmJ 1 Reply Last reply
    0
    • S spomm

      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
      4033cb12-499e-49c6-a092-0f6fb8ab5ce6-image.png

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      0
      • jsulmJ jsulm

        @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.

        S Offline
        S Offline
        spomm
        wrote on last edited by
        #3

        @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); 
        

        }`

        1 Reply Last reply
        0
        • S Offline
          S Offline
          spomm
          wrote on last edited by
          #4

          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 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved