Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

fragmentation error when using Qtcpsocket write



  • hello
    i write a application that connects to server throw Qtcp socket.
    this program works well```

    but in send message method, fragmentation error cures.
    thanks for your help.

    TcpClient::TcpClient(QObject *parent) : QObject(parent)
    {
    socket = new QTcpSocket();
    qDebug()<<socket;
    }
    
    bool TcpClient::connectClient(QString ip, quint16 port)
    {
    
          connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
          connect(socket, SIGNAL(readyRead()), this, SLOT(readReady()));
            qDebug() << "Connecting,..";
            socket->connectToHost(ip, port);
            qDebug()<<"connected";
            qDebug()<<socket;
            return socket->waitForConnected();
    }
    
    void TcpClient::sendMessage(QString message)
    {
        //sending message to server
         if(socket->state() == QAbstractSocket::ConnectedState)
             {
             qDebug()<<socket;
        socket->write(message.toUtf8());
         }
    
    }
    
    
    void TcpClient::readReady()
    
    {
    
        QByteArray data = socket->readAll();
        QString str(data);
        str=emit manageRequest(str);
        if(str!=""){
            socket->write(str.toUtf8());
        }
    
    
    }
    
    void TcpClient::disconnected()
    {
    qDebug() << "disconnected " ;
    }
    
    

  • Lifetime Qt Champion

    @ali910 said in fragmentation error when using Qtcpsocket write:

    fragmentation error cures.

    What does this mean?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    From the looks of it you are expecting your client to receive all the data you sent in one go. That's not how tcp works. It's up to you cumulate the data you receive contains a full frame.

    You can use QDataStream and transactions.


Log in to reply