Results 1 to 14 of 14

Thread: Problems with TCP Raw Data

  1. #1
    Join Date
    Feb 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems with TCP Raw Data

    Dears,

    I am writing a server application for some GPS devices and I am having trouble with the receiving data.

    The server is working fine, it receives the header as expected and writes to the device, but the data expected to be received is as shown:

    Qt Code:
    1. 080400000113fc208dff000f14f650209cca80006f00d6040004000403010115031603000
    2. 1460000015d0000000113fc17610b000f14ffe0209cc580006e00c0050001000403010115
    3. 0316010001460000015e0000000113fc284945000f150f00209cd20000950108040000000
    4. 4030101150016030001460000015d0000000113fc267c5b000f150a50209cccc000930068
    5. 0400000004030101150016030001460000015b0004
    To copy to clipboard, switch view to plain text mode 

    but the data received is:

    Qt Code:
    1. 7811a7b728990508100000001100000098020508000000000000000069000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001100000058060508000000000000000011000000000000000000000000000000110000006877e3b7000000000000000011000000e874e3b7000000000000000011000000a873e3b7000000000000000011000000a873e3b7010000000000000021000000c80905080040a7b780a2f8b700000000c80905080040a7b780a2f8b71100000002000000010000000000000011000000a873e3b7020000000000000011000000a873e3b7030000000000000039000000c803050802000000c8090508801c5eebe01e5eebd01e5eeb2f7573722f6c69622f67636f6e762f5554462d31362e736f0000000011000000a873e3b7040000000000000011000000a873e3b70500000000000000190000001404050808000000a80c05080900000001000000290000000100000003000000020000000300000001000000e80005083330322f80d4c0b7b4b0e3b711000000a873e3b7060000000000000011000000a873e3b707000000000000003900000001000000040000000300000001000000
    To copy to clipboard, switch view to plain text mode 

    and the slot (connected to readyRead() signal) that I am handling the received data is as shown:

    Qt Code:
    1. // Just for verification
    2. qDebug() << "Bytes Available: " << tcpClient->bytesAvailable();
    3.  
    4. QDataStream in(tcpClient);
    5. quint16 header;
    6.  
    7. in >> header; // reading header
    8.  
    9. char *data = new char[header];
    10.  
    11. // reading raw data on the char pointer above
    12. in.readRawData(data, header);
    13.  
    14. QByteArray b(data, tcpClient->bytesAvailable());
    15.  
    16. // displaying "crazy" data as shown above
    17. printf(b.toHex());
    18.  
    19.  
    20. // sending accepted message to the device
    21. // otherwise the device won't send you the data back
    22. QDataStream out(&ba, QIODevice::WriteOnly);
    23. out << true;
    24.  
    25. tcpClient->write(ba);
    To copy to clipboard, switch view to plain text mode 

    Any suggestions what I am doing wrong?

    Sincerely,
    Milot.

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with TCP Raw Data

    1. change in >> header; to
    Qt Code:
    1. in.readRawData( &header, sizeof( header ) );
    To copy to clipboard, switch view to plain text mode 
    to avoid read header as text
    2. after in.readRawData( data, header ); tcpClient->bytesAvailable() seems to be not equal to header
    try
    Qt Code:
    1. QByteArray b( data, header )
    To copy to clipboard, switch view to plain text mode 
    3. I think it would be better to use QIODevice's peek function to retrieve header, then look if it greater, then tcpClient->bytesAvailable(), and ( if it is ) go further

  3. #3
    Join Date
    Feb 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with TCP Raw Data

    Hi borisbn,

    Thank you for your reply. I was testing until now still no luck.

    As per your suggestions, the signature of readRawData is readRawData(char *s, int l); and in my code header is quint16 so I cannot use that. I tried replacing bytesAvailable() with header, but no luck because I keep getting header 0 and bytesAvailable() 959!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with TCP Raw Data

    Why are you using QDataStream?

  5. #5
    Join Date
    Feb 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with TCP Raw Data

    Because of this statement: The QDataStream class provides serialization of binary data to a QIODevice. and the readRawData() function, and I am thinking that this class is helping me...

    Do you have any other suggestion?

    Thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with TCP Raw Data

    Quote Originally Posted by milot View Post
    Because of this statement: The QDataStream class provides serialization of binary data to a QIODevice.
    Was the data serialized on the other end with QDataStream as well?

  7. #7
    Join Date
    Feb 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with TCP Raw Data

    Yes,

    Because the devices wait for response from the server, so I sent the response.

    First I am able to get the IMEI number from the device using this code:

    Qt Code:
    1. quint16 hd;
    2.  
    3.  
    4. QDataStream in(tcpClient);
    5. in >> hd;
    6.  
    7. qDebug() << "Header: " << hd;
    8.  
    9. char *data = new char[hd];
    10. in.readRawData(data, hd);
    11.  
    12. QString str = QString::fromUtf8(data);
    13.  
    14. qDebug() << "IMEI: " << str;
    To copy to clipboard, switch view to plain text mode 

    The device first sends its IMEI number and waits for response codes 00 (that is deny and it stops sending packets) and 01 (server tells the device to proceed with other info). I send this packet to the device using the following:

    Qt Code:
    1. QDataStream out(&ba, QIODevice::WriteOnly);
    2. out << true;
    3.  
    4. tcpClient->write(ba);
    To copy to clipboard, switch view to plain text mode 

    Then the first output is like this:

    Qt Code:
    1. New connection from: "91.187.105.189"
    2. Header: 15
    3. IMEI: "352848021535568"
    To copy to clipboard, switch view to plain text mode 

    after the server sends the 01 (boolean value in my case) to the device, I get this:

    Qt Code:
    1. Header: 0
    2. IMEI: "x!��"
    To copy to clipboard, switch view to plain text mode 

    Which seems to be very strange!

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with TCP Raw Data

    How does the transmission look like on the transmitting end? Mixing serialized and raw data in QDataStream is a bad idea in general, by the way. Also why doesn't the data stream work directly on the socket but instead it operates on some byte array that is then transmitted through the socket? I fail to see the point of using QDataStream in such a situation.

  9. #9
    Join Date
    Feb 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with TCP Raw Data

    I am not able to see the other end because it is a GPS device that transmits and receives data.

    So are you suggesting that I should read the packets using only readRawData()?

    I cannot find enough documentation on how to treat such situations, if you have something worth sharing I would be more than happy

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with TCP Raw Data

    Quote Originally Posted by milot View Post
    I am not able to see the other end because it is a GPS device that transmits and receives data.
    And it uses QDataStream??????????????????????????? I never heard of a GPS device using QDataStream as its communication protocol... Are you sure of that? Or have I misunderstood your "yes" answer to my question about whether the data was serialized using QDataStream?

  11. #11
    Join Date
    Feb 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with TCP Raw Data

    It uses TCP as communication protocol, for that reason I am trying to read from QTcpSocket using QDataStream!

    As far as you can see on my example above, it receives data as expected and sends the data back to the device, because if I don't send data back to the device I am not able to receive any more data. For sending and receiving I data I'm using QDataStream, I pasted code above that I am able to read the header using QDataStream and write to the device using writeRawBytes() function.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with TCP Raw Data

    QDataStream is not a general purpose stream for reading and writing non-textual data. It is a serialization mechanism with its own protocol. You can't use it to read data from an arbitrary binary stream. Using only readRawBytes() and writeRawBytes() with QDataStream only adds overhead without giving anything in return. If you know how many bytes to read then just read them and interpret them the way the other end of the communication expects you to.
    Last edited by wysota; 12th April 2010 at 14:37.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Feb 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with TCP Raw Data

    Thank you for the clarification.

    Now I am avoiding using QDataStream, because in general serialization purposes I use QDataStream very often, and after reading that it has support for readRawData I thought it was the way to go.

    Thank you again, whenever I come with a solution I will inform the others in this post as well.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with TCP Raw Data

    Your problem is probably that you are going out of sync somewhere with your data. You have to remember you can't just read all data that is available in the socket and expect it to contain exactly as many bytes as you would want. Data needs to be buffered and reading needs to be deferred to a time when you are sure the buffer contains all the data you need.

Similar Threads

  1. Problems with maximum data sent over a QTcpSocket
    By jordip in forum Qt Programming
    Replies: 8
    Last Post: 6th July 2010, 14:34
  2. Some problems
    By Dante in forum Qt Programming
    Replies: 13
    Last Post: 20th July 2009, 14:38
  3. Corrupt JPEG data: premature end of data segment
    By node_ex in forum Qt Programming
    Replies: 1
    Last Post: 19th August 2008, 08:57
  4. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39
  5. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.