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. <Solved> TCP client read wrong data from server
Forum Updated to NodeBB v4.3 + New Features

<Solved> TCP client read wrong data from server

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcpserverqtcpsocket
4 Posts 2 Posters 1.5k 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.
  • V Offline
    V Offline
    Vitek
    wrote on last edited by Vitek
    #1

    Hi, i created tcp server and client. Server send data to clients, but client read wrong data. Please help me.

    This slot conneted with signal readyRead() at a client.

    
    void Client::readSocketData()
    {
        QDataStream in(m_socket);
        while(true)
        {
            if (m_nextBlockSize == 0)
            {
                if (m_socket->bytesAvailable() < sizeof(quint16(0)))
                    return;
                in >> m_nextBlockSize;
                qDebug() << m_nextBlockSize;
            }
            if (m_socket->bytesAvailable() < m_nextBlockSize)
            {
                return;
            }
            QString text;
            in >> text;
            qDebug() << text;
            m_nextBlockSize = 0;
        }
    }
    

    client class for server

    ClientConnection::ClientConnection(int socketDescriptor, QObject *parent) : QThread(parent),
        m_socketDescriptor(socketDescriptor)
    {
     
    }
     
    void ClientConnection::writeData(const QString& text)
    {
        QByteArray rawData;
        QDataStream data(&rawData, QIODevice::ReadWrite);
        data << quint16(0) << text;
        data.device()->seek(0);
        data << quint16(rawData.size() - sizeof(quint16));
     
        m_socket->write(rawData);
        m_socket->waitForBytesWritten();
    }
     
    void ClientConnection::run()
    {
        qDebug() << "New connection";
        m_socket = new QTcpSocket();
        m_socket->setSocketDescriptor(m_socketDescriptor);
    }
    

    Class Server

    
    MyServer::MyServer(QObject *parent) : QTcpServer(parent)
    {
        if(!this->listen(QHostAddress::Any, 1234))
        {
            qDebug() << "Could not start server";
        }
        else
        {
            qDebug() << "Listening...";
        }
    }
     
    void MyServer::sendText(const QString &text)
    {
        for(int i = 0; i < m_connections.size(); ++i)
        {
            m_connections.at(i)->writeData(text);
        }
    }
     
    void MyServer::incomingConnection(int handle)
    {
        ClientConnection *client = new ClientConnection(handle);
        client->start();
        m_connections.push_back(client);
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What do you mean by wrong data ? What do you expect ? What do you get ?

      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
      0
      • V Offline
        V Offline
        Vitek
        wrote on last edited by Vitek
        #3

        Thank for reply.
        I solve my problem. i sent my block size as quint16 but i read as int.

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

          Nice you found out and thanks for sharing :)

          There's no need to modify the title anymore to mark the thread as solved, you can use the "Topic Tool" button for that :)

          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
          0

          • Login

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