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. I have a problem of abnormal message when using qt socket
Forum Updated to NodeBB v4.3 + New Features

I have a problem of abnormal message when using qt socket

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.7k 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.
  • C Offline
    C Offline
    Charles Zhang
    wrote on 1 Jan 2018, 09:25 last edited by
    #1

    Hi, I'm using qtsocket to write a client-server project. I have a client to send message and a server to process those message. But when the processing time is long, the messages that the server receives become abnormal and I can't read normal data sometimes.

    The phenomenon is that I get normal message and intermittently some abnormal message. The abnormal message does not affect the following message. When I reduce the processing time, all the message becomes normal. Here is some of my code:

    Client

    QByteData message;
    QDataStream out(&message, QIODevice::WriteOnly);
    out<< Some Data
    socket->write(message);
    socket->flush();
    

    Server:

    //These codes are in ReadData() function
    QDataStream in(socket);
    if( block_size==0 ){
        if( socket->bytesAvailable() < (int)sizeof(quint16) )
             return;
        in>>block0_size;
    }
    if( socket->bytesAvailable() < block_size ){
        return;
    }
    in>> Read message
    Call processing function
    block_size = 0;
    

    I guess the problem is that when processing function runs for a long time, there are a lot of messages coming and buffer isn't large enough. But I don't know how to verify this hypothesis and neither how to fix it.

    A 1 Reply Last reply 1 Jan 2018, 20:42
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 1 Jan 2018, 19:34 last edited by
      #2

      Hi,

      What is a normal and an abnormal message for you ?

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

      C 1 Reply Last reply 2 Jan 2018, 04:26
      0
      • C Charles Zhang
        1 Jan 2018, 09:25

        Hi, I'm using qtsocket to write a client-server project. I have a client to send message and a server to process those message. But when the processing time is long, the messages that the server receives become abnormal and I can't read normal data sometimes.

        The phenomenon is that I get normal message and intermittently some abnormal message. The abnormal message does not affect the following message. When I reduce the processing time, all the message becomes normal. Here is some of my code:

        Client

        QByteData message;
        QDataStream out(&message, QIODevice::WriteOnly);
        out<< Some Data
        socket->write(message);
        socket->flush();
        

        Server:

        //These codes are in ReadData() function
        QDataStream in(socket);
        if( block_size==0 ){
            if( socket->bytesAvailable() < (int)sizeof(quint16) )
                 return;
            in>>block0_size;
        }
        if( socket->bytesAvailable() < block_size ){
            return;
        }
        in>> Read message
        Call processing function
        block_size = 0;
        

        I guess the problem is that when processing function runs for a long time, there are a lot of messages coming and buffer isn't large enough. But I don't know how to verify this hypothesis and neither how to fix it.

        A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 1 Jan 2018, 20:42 last edited by
        #3

        @Charles-Zhang

        In principle there are three places where the error can be:

        1. in the client code
        2. in the server code
        3. somewhere in between (O/S or Qt)

        3 will be very unlikely. and buffer overrun even more, as Qt uses unlimited buffers by default.

        So I'd run Wireshark to see if the data looks as expected "on the wire". that should give you an hint where to investigate further - client or server side.

        Happy New Year!

        Qt has to stay free or it will die.

        1 Reply Last reply
        0
        • S SGaist
          1 Jan 2018, 19:34

          Hi,

          What is a normal and an abnormal message for you ?

          C Offline
          C Offline
          Charles Zhang
          wrote on 2 Jan 2018, 04:26 last edited by
          #4

          @SGaist I use a self-defined protocol, which contains message length, message type and some data and they are normal message. The message type of abnormal message is right, but the message type become messy and so does data.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 2 Jan 2018, 08:42 last edited by
            #5

            if( block_size==0 ) vs in>>block0_size;

            I guess it's just a typo. 2 notes:

            • since Qt 5.7 you don't need that block_size magic any more and you can just use QDataStream::startTransaction/QDataStream::commitTransaction. See the Client::readFortune() method of the fortune example
            • you need to show us what's going on in out<< Some Data and in>> Read message (the former is my prime suspect)

            "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

            C 1 Reply Last reply 3 Jan 2018, 08:03
            1
            • V VRonin
              2 Jan 2018, 08:42

              if( block_size==0 ) vs in>>block0_size;

              I guess it's just a typo. 2 notes:

              • since Qt 5.7 you don't need that block_size magic any more and you can just use QDataStream::startTransaction/QDataStream::commitTransaction. See the Client::readFortune() method of the fortune example
              • you need to show us what's going on in out<< Some Data and in>> Read message (the former is my prime suspect)
              C Offline
              C Offline
              Charles Zhang
              wrote on 3 Jan 2018, 08:03 last edited by
              #6

              @VRonin I'm sorry, it's a typo error. I use Qt 5.2.1 so I can't use QDataStream::startTransaction.

              My message is the position and lidar data of a smart car and let me show the details.
              Client:

              QByteArray message;
              QDataStream out(&message, QIODevice::WriteOnly);
              out<<(quint16)0;
              out<<message_type; // message_type means the type of this message, for this lidar data message it is a fixed number
              long long timestamp=lidarData->timestamp;
              out<<timestamp; //Timestamp of this message
              out<<self_num; //It's the car number
              int size = lidarData->datasize; // The size of lidar data array
              out<<size;
              out<<x<<y<<ori;
              for( int i=0; i<size; i++ )
                  out<<lidarData->data[i]; // lidar data
              out.device()->seek(0);
              out << (quint16)(message.size() - sizeof(quint16)); //The length of this message
              socket_host->write(message);
              socket_host->flush();
              

              Server:

              //Receive message
                  QDataStream in(socket);
                  if( block_size==0 ){
                      if( socket->bytesAvailable() < (int)sizeof(quint16) )
                          return;
                      in>>block_size;
                  }
                  if( socket->bytesAvailable() < block_size ){
                      return;
                  }
              //Allocate local data
                  short data[361]; //This 361 equals lidarData->datasize, which is lidar data size
                  long long timestamp;
                  quint32 from_num;
                  int size;
                  float x, y, orien;
                  quint32 message_type;
              //Read message
                  in>>message_type;
                  if( message_type==1 ){ //if message_type is lidar data message
                      in>>timestamp>>from_num>>size>>x>>y>>orien;
                      for( int i=0; i<size; i++ ){
                          in>>data[i];
                      }
                      qDebug()<<"Receive message from "<<from_num<<" message type "<<message_type<<",timestamp "<<timestamp
                             <<", message size is "<<block0_size<<", x "<<x<<", y "<<y<<", ori "<<orien
                             <<" data size "<<size<<" bytesAvailable "<<socket0->bytesAvailable();
                      UpdateMap(x, y, orien, size, from_num, data);
                  }
                  else{
                      qDebug()<<"Message type not 1";
                  }
              

              The problem is about UpdateMap() function. If the process time of that function is short, all the message can be safely received. But if the time is long, the message_type of some message changes, it is not 1 and it becomes a huge negative number. All the data following the message_type becomes huge messy number.

              1 Reply Last reply
              0
              • V Offline
                V Offline
                VRonin
                wrote on 3 Jan 2018, 08:36 last edited by
                #7
                • Given the problem it would be nice if you can add a comment specifying the type of every variable
                • short and long long are not size safe. use qint16 and qint64 instead.
                • data[361] and in>>data[i]; is, in general, unsafe you might go out of bounds
                • if you receive anything that is not message_type==1 you keep in the buffer all the rest
                • Where are these slots called from? are you using multiple threads?

                "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

                1/7

                1 Jan 2018, 09:25

                • Login

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