Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. fragmentation error when using Qtcpsocket write
Forum Updated to NodeBB v4.3 + New Features

fragmentation error when using Qtcpsocket write

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 303 Views 1 Watching
  • 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.
  • A Offline
    A Offline
    ali910
    wrote on last edited by
    #1

    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 " ;
    }
    
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ali910 said in fragmentation error when using Qtcpsocket write:

      fragmentation error cures.

      What does this mean?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

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