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. QTcpSocket received data type

QTcpSocket received data type

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 690 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    How to distinguish received data type of QTcpSocket ?

    {
        tcpServer = new QTcpServer(this);
        tcpServer->listen(QHostAddress::Any, 9999);
        connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnect()));
    }
    
    void MainWindow::newConnect()
    {
        tcpSocket = tcpServer->nextPendingConnection();
        connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readMessage()));
    }
    
    void MainWindow::readMessage()
    {
        qint64 len = tcpSocket->bytesAvailable();
        QByteArray BA = tcpSocket->read(len);
        //QString type = ?
        QLabel *label = new QLabel;
        if(type == "text"){
            QTextCodec *utf8codec = QTextCodec::codecForName("UTF-8");    
            QString utf8str = utf8codec->toUnicode(BA);
            label->setText(utf8str);
        }else if(type == "image"){
            QPixmap pixmap;
            pixmap.loadFromData(BA);
            label->setPixmap(pixmap);
        }
    }
    

    https://github.com/sonichy

    K 1 Reply Last reply
    0
    • sonichyS sonichy

      How to distinguish received data type of QTcpSocket ?

      {
          tcpServer = new QTcpServer(this);
          tcpServer->listen(QHostAddress::Any, 9999);
          connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnect()));
      }
      
      void MainWindow::newConnect()
      {
          tcpSocket = tcpServer->nextPendingConnection();
          connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readMessage()));
      }
      
      void MainWindow::readMessage()
      {
          qint64 len = tcpSocket->bytesAvailable();
          QByteArray BA = tcpSocket->read(len);
          //QString type = ?
          QLabel *label = new QLabel;
          if(type == "text"){
              QTextCodec *utf8codec = QTextCodec::codecForName("UTF-8");    
              QString utf8str = utf8codec->toUnicode(BA);
              label->setText(utf8str);
          }else if(type == "image"){
              QPixmap pixmap;
              pixmap.loadFromData(BA);
              label->setPixmap(pixmap);
          }
      }
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @sonichy

      As you have to do with any other comm lib. You need know the format to be received respectively the device has to tell you what to expect.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        Distinguishing the data type is not your problem here.
        Have a look at this example, especially the ChatClient::sendMessage and ChatClient::onReadyRead methods.

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        sonichyS 1 Reply Last reply
        3
        • VRoninV VRonin

          Distinguishing the data type is not your problem here.
          Have a look at this example, especially the ChatClient::sendMessage and ChatClient::onReadyRead methods.

          sonichyS Offline
          sonichyS Offline
          sonichy
          wrote on last edited by
          #4

          @VRonin Thanks, you use JSON to set type and content !

          QJsonObject message;
          message["type"] = QStringLiteral("message");
          message["text"] = text;
          

          https://github.com/sonichy

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            I do but doesn't really matter, you can even write directly to the datastream clientStream << QStringLiteral("message") << text; as long as the receiver also uses the same structure to read. The key takeaway form that example is how to send and receive data to deal with cases in which partial data is received or more than 1 message is received at once

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2

            • Login

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