Results 1 to 20 of 25

Thread: Formatting a packet with different types then send it over TCP

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2011
    Posts
    64
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 2 Times in 2 Posts

    Default Re: Formatting a packet with different types then send it over TCP

    What does it have to do with anything?
    This change the image of how hexadecimal represent it in 32-bit machine.
    You are still using QDataStream, which is not what you really want.
    Would you suggest a solution fit my need, I made this try. It looks send the formatted packet as I expect but I don't now how to parse it correctly in other side:
    Qt Code:
    1. void GuiClient::sendMsg()
    2. {
    3.  
    4. int n = ui->msgEdit->text().toAscii().trimmed().size();
    5.  
    6. msg.append(static_cast<unsigned char> (PROTO_VERSION));
    7.  
    8. for(int i2 = 0; i2 != sizeof(n); ++i2)
    9. {
    10. msg.append((char)(n&(0xFF << i2) >>i2));
    11. }
    12.  
    13. msg.append(static_cast<unsigned char>(MSG_TYPE));
    14. msg.append(ui->msgEdit->text().toAscii().trimmed());
    15.  
    16.  
    17. qDebug("%d", msg.size());
    18. qDebug("%s", msg.data());
    19.  
    20. client->write(msg);
    21.  
    22. QFile f("test0" + QString::number(++i) + ".bin");
    23. if (f.open(QIODevice::WriteOnly)) {
    24. f.write(msg);
    25. f.close();
    26. }
    27.  
    28. ui->msgEdit->clear();
    29. ui->msgEdit->setFocus();
    30. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void GuiServer::readMsg()
    2. {
    3. if(client) //user stored member variable, should be 0 if no client is connected
    4. {
    5. int blockSize = 0;
    6.  
    7. while (client->bytesAvailable() < (int)sizeof(int) + 2) {
    8. if (!client->waitForReadyRead(2000)) {
    9. //emit error(client->error(), client->errorString());
    10. return;
    11. }
    12. }
    13.  
    14. QByteArray array(client->read(6));
    15.  
    16. QByteArray array2;
    17.  
    18. array2[0] = array[1];
    19. array2[1] = array[2];
    20. array2[2] = array[3];
    21. array2[3] = array[4];
    22.  
    23. memcpy(&blockSize, array2, sizeof(int));
    24.  
    25. int i = 0;
    26.  
    27. QFile f("test0" + QString::number(++i) + ".bin");
    28. if (f.open(QIODevice::WriteOnly)) {
    29. f.write(array);
    30. f.close();
    31. }
    32.  
    33. qDebug("msg %s", array.data());
    34. qDebug("protocol %s", array.data()[0]);
    35. qDebug("size %d", blockSize);
    36. qDebug("message type %s", array.data()[5]);
    37.  
    38. while (client->bytesAvailable() < blockSize) {
    39. if (!client->waitForReadyRead(2000)) {
    40. //emit error(socket.error(), socket.errorString());
    41. return;
    42. }
    43. }
    44.  
    45. QString msg;
    46.  
    47. ui->historyEdit->append(msg);
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 
    Hexdump shows:
    Client side:
    $ hexdump -Cb test01.bin
    Qt Code:
    1. 00000000 a7 05 05 05 05 a8 65 72 74 |......ert|
    2. 0000000 247 005 005 005 005 250 145 162 164
    3. 0000009
    To copy to clipboard, switch view to plain text mode 
    Server side:
    Qt Code:
    1. 00000000 a7 05 05 05 05 a8 |......|
    2. 0000000 247 005 005 005 005 250
    3. 0000006
    To copy to clipboard, switch view to plain text mode 
    It seems like the server can read the size of packet but it crashes when it tries to read data.
    I am in this project since week(trying to implement my protocol), google tired from me, and I am not sleep well because this(its like nightmare).

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

    Default Re: Formatting a packet with different types then send it over TCP

    Quote Originally Posted by SIFE View Post
    This change the image of how hexadecimal represent it in 32-bit machine.
    No, it doesn't. The datatype states it is a "32 bit integer", it has nothing to do with its hexadecimal representation. You can as well represent it as 32 binary digits. Hex, bin or dec are just interpretations of the actual data stored in the computer memory according to internal representation chosen by the CPU and its compiler.

    I made this try.
    A very good try. You should have done that in the beginning.
    It looks send the formatted packet as I expect but I don't now how to parse it correctly in other side
    Exactly the same way you assemble the message on the sending side.

    If you send two bytes here, then you need to extract two bytes there, etc.

    I am in this project since week(trying to implement my protocol), google tired from me, and I am not sleep well because this(its like nightmare).
    Then take a piece of paper and draw a scheme of how your protocol is supposed to work and the bit-by-bit representation of its PDU. If you don't know how to do it, have a look at diagrams of standard binary protocols such as IP or NTP. If you google for "IP packet diagram", something meaningful will pop up. Practise with a raw stream of bytes until you learn to detect a beginning and end of each protocol unit in the raw stream. If you can't do it then probably the protocol lacks some necessary information and you have to adjust it accordingly.

    Only when you fully understand how your protocol should work (and why this and not some other way), then you may start writing an implementation. If you expect to talk to computers with different architecture, pay special attention to the byte order of words.
    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.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Formatting a packet with different types then send it over TCP

    The binary dumps show the size figure as "05 05 05 05" (hex), or 84 215 045 bytes, with a sent text message length of just three bytes. Something is wrong with your sending logic (Hint: shift by bytes not bits). Perhaps you should research the htonl() and ntohl() macros/functions.

    This thread is a reminder of why major high-level network protocols (SMTP, HTTP, NNTP, POP3, IMAP) are text-based and not binary. Not as efficient but a lot easier to get right.

  4. #4
    Join Date
    Feb 2011
    Posts
    64
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 2 Times in 2 Posts

    Default Re: Formatting a packet with different types then send it over TCP

    Then take a piece of paper and draw a scheme of how your protocol is supposed to work and the bit-by-bit representation of its PDU. If you don't know how to do it, have a look at diagrams of standard binary protocols such as IP or NTP. If you google for "IP packet diagram", something meaningful will pop up. Practise with a raw stream of bytes until you learn to detect a beginning and end of each protocol unit in the raw stream. If you can't do it then probably the protocol lacks some necessary information and you have to adjust it accordingly.

    Only when you fully understand how your protocol should work (and why this and not some other way), then you may start writing an implementation. If you expect to talk to computers with different architecture, pay special attention to the byte order of words.
    I already did that, now I am in the implementation, just because i am new to qt network programming.
    Now I have a problem in receiving data:
    Qt Code:
    1. void GuiServer::readMsg()
    2. {
    3. if(client) //user stored member variable, should be 0 if no client is connected
    4. {
    5. int blockSize = 0;
    6.  
    7. while (client->bytesAvailable() < (int)sizeof(int) + 2) {
    8. if (!client->waitForReadyRead(2000)) {
    9. //emit error(client->error(), client->errorString());
    10. return;
    11. }
    12. }
    13.  
    14. QByteArray data(client->read(6));
    15.  
    16. QByteArray array2;
    17. array2.reserve(4);
    18. array2[0] = data[1];
    19. array2[1] = data[2];
    20. array2[2] = data[3];
    21. array2[3] = data[4];
    22.  
    23. memcpy(&blockSize, array2, sizeof(int));
    24.  
    25. /* for (int j = 0; j < 4; ++j)
    26.   blockSize |= ((int)data[j] & 0x000000ff) << (j * 8);*/
    27.  
    28.  
    29. QFile f("test0" + QString::number(++i) + ".bin");
    30. if (f.open(QIODevice::WriteOnly)) {
    31. f.write(data);
    32. f.close();
    33. }
    34.  
    35. qDebug("size %d", blockSize);
    36. /*qDebug("msg %s", data.data());
    37.   qDebug("protocol %s", data.data()[0]);
    38.   qDebug("message type %s", data.data()[5]);
    39. */
    40. while (client->bytesAvailable() < blockSize) {
    41. if (!client->waitForReadyRead(2000)) {
    42. return;
    43. }
    44. }
    45.  
    46. QString msg(data);
    47.  
    48. ui->historyEdit->append(msg);
    49. }
    50. }
    To copy to clipboard, switch view to plain text mode 
    six-test.JPG
    I sent six messages from client, they all sent correctly however in the are side it seems like I receive the length first and if i sent another message from the client I receive the body. The photo from attachment shows how the server receive each message.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Formatting a packet with different types then send it over TCP

    So what happens in the second while loop if all your bytes arrived at once and were available when you entered the routine? What about if only some of the message bytes have been received? What if some of the bytes are delayed?

    Have you set a breakpoint and single-stepped this?
    Last edited by ChrisW67; 10th June 2011 at 02:48.

  6. #6
    Join Date
    Feb 2011
    Posts
    64
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 2 Times in 2 Posts

    Default Re: Formatting a packet with different types then send it over TCP

    The first loop to read 6 bytes(2 bytes + 4 bytes of size), in the second loop I block until I read the whole packet, so this all I get. If you have better solution please post it.

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

    Default Re: Formatting a packet with different types then send it over TCP

    Quote Originally Posted by SIFE View Post
    in the second loop I block until I read the whole packet
    What if two packets arrive at once?

    If you have better solution please post it.
    My universal approach is to use signals and slots, buffer incoming data and process the buffer in a while loop as long as there is enough data in it.
    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.


  8. #8
    Join Date
    Feb 2011
    Posts
    64
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 2 Times in 2 Posts

    Default Re: Formatting a packet with different types then send it over TCP

    My universal approach is to use signals and slots, buffer incoming data and process the buffer in a while loop as long as there is enough data in it.
    Would you say it in code .

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

    Default Re: Formatting a packet with different types then send it over TCP

    If you want code then search the forum. I can give you a short explanation -- connect to the socket's readyRead() signal, append all pending data to your own buffer and process the buffer in a while loop according to the following pseudo-code logic:
    Qt Code:
    1. buffer += socket.readAll();
    2. while(buffer.size >= expectedBlockSize) {
    3. data = buffer.take(expectedBlockSize);
    4. process(data);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Of course expectedBlockSize will depend on what you expect to receive.
    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.


Similar Threads

  1. MAC address of UDP packet's sender
    By mastupristi in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2011, 13:12
  2. Replies: 4
    Last Post: 10th December 2009, 16:37
  3. Packet getting fragmented when sending over QTcpSocket
    By arturo182 in forum Qt Programming
    Replies: 14
    Last Post: 5th August 2009, 23:11
  4. how to send ICMP packet
    By eleanor in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2007, 14:23
  5. QtNetwork send ICMP Packet
    By SlowTree in forum Qt Programming
    Replies: 3
    Last Post: 29th October 2007, 20:13

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.