Results 1 to 3 of 3

Thread: Mixing QUdpSocket and Winsocks - Weird output

  1. #1
    Join Date
    Feb 2018
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Exclamation Mixing QUdpSocket and Winsocks - Weird output

    Hey guys,

    I am not sure what is going on here or if the issue could be easily fixed or not. It might be a basic thing that I am missing.
    I need to send some data over network UDP sockets so I built a "sender" and a "receiver" using QUdpSocket which worked fine.
    The requirements changed and now the "sender" has to send data using Winsocks instead QUdpSocket but the "receiver" still can used QUdpSocket.
    I am getting a weird output when using qDebug on the received stream. I have put together an example about how I am doing everything and
    hopefully, someone will have the answer.



    ######### SENDING - #1
    stringstream str;

    str << (uint32)0;

    str << (uint32)0x53486472 <<
    (uint32) << 0
    (uint32)1 <<
    (uint32)2 <<
    (uint32)3 <<
    (uint32)4 <<
    (uint32)5 <<
    (uint32)6 <<
    (uint32)7;

    str << (uint32)0x54695355 <<
    (char*)INFO-1 <<
    (char*)869543-1 <<
    (char*)85A-1 <<
    (uint16)1 <<
    (char*)C0B3-MV1R-1 <<
    (bool)false;



    char * sendbuf = new char[str.str().length() + 1];
    std::strcpy(sendbuf, str.str().c_str());


    struct sockaddr_in clientService;
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr(address);
    clientService.sin_port = htons(port);


    SOCKET sock = socket(clientService.sin_family, SOCK_DGRAM, IPPROTO_UDP);
    connect(sock, (SOCKADDR*)&clientService, sizeof(clientService));
    send(sock, sendbuf, (int)strlen(sendbuf), 0);
    closesocket(sock);



    ######### SENDING - #2

    QByteArray datagram;
    QDataStream stream(&datagram, QIODevice::WriteOnly);
    stream.setVersion(QDataStream::Qt_5_9);

    stream << (uint32)0;

    stream << (uint32)0x53486472 <<
    (uint32) << 0
    (uint32)1 <<
    (uint32)2 <<
    (uint32)3 <<
    (uint32)4 <<
    (uint32)5 <<
    (uint32)6 <<
    (uint32)7;

    stream << (uint32)0x54695355 <<
    (char*)INFO-1 <<
    (char*)869543-1 <<
    (char*)85A-1 <<
    (uint16)1 <<
    (char*)C0B3-MV1R-1 <<
    (bool)false;


    QUdpSocket *udpSocket = new QUdpSocket(this);
    udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::LocalHost, port);



    ######### RECEIVING

    QUdpSocket *udpSocket = new QUdpSocket(this);

    QByteArray datagram;
    qint64 size = udpSocket->pendingDatagramSize();
    datagram.resize(size);
    udpSocket->readDatagram(datagram.data(), datagram.size());

    QDataStream stream(&datagram, QIODevice::ReadOnly);
    stream.setVersion(QDataStream::Qt_5_9);

    qDebug() << "NetworkReceiver";
    qDebug() << datagram.size();
    qDebug() << datagram;





    -------- Output when using SENDING #1:
    NetworkReceiver
    61
    "01397253234012345671416188757INFO-1869543-185A-11C0B3-MV1R-10"




    -------- Output when using SENDING #2:
    NetworkReceiver
    97
    "\x00\x00\x00\x00SHdr\x00\x00\x00\x00\x00\x00\x00\ x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x0 4\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x07T iSU\x00\x00\x00\x07INFO-1\x00\x00\x00\x00\t869543-1\x00\x00\x00\x00\x06""85A-1\x00\x00\x01\x00\x00\x00\fC0B3-MV1R-1\x00\x00"

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mixing QUdpSocket and Winsocks - Weird output

    Hi, I think the difference is that in "SENDING - #1" you use a stringstream while in "SENDING - #2" you use a QDataStream. The QDataStream will encode data depending on the version used in setVersion.

    Ginsengelf

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

    Default Re: Mixing QUdpSocket and Winsocks - Weird output

    QDataStream outputs sufficient information to reassemble the result at the other end.
    The uint32 values are 4 bytes each in a byte dictated by settings on the stream.
    When you stream "INFO-1" you get a 4-byte size (00 00 00 07) followed by the 6 bytes of the string and a zero byte
    The bool is streamed as a two byte integer.

    The stringstream converts the numeric values to decimal string, so:
    Qt Code:
    1. #include <cstdint>
    2. #include <iostream>
    3. #include <sstream>
    4.  
    5. int main(int argc, char **argv) {
    6. std::stringstream str;
    7. str << (uint32_t)0; // "0" in output
    8. str << (uint32_t)0x53486472 // "1397253234" in output i.e. the number in decimal as a string
    9. << (uint32_t)0 // "0"
    10. << (uint32_t)1 // "1"
    11. << (uint32_t)2 // "2"
    12. << (uint32_t)3 // "3"
    13. << (uint32_t)4 // "4"
    14. << (uint32_t)5 // "5"
    15. << (uint32_t)6 // "6"
    16. << (uint32_t)7; // "7"
    17.  
    18. str << (uint32_t)0x54695355 // "1416188757"
    19. << (char*)"INFO-1"
    20. << (char*)"869543-1"
    21. << (char*)"85A-1"
    22. << (uint16_t)1 // "1"
    23. << (char*)"C0B3-MV1R-1"
    24. << (bool)false; // "0"
    25.  
    26. std::cout << str.str() << std::endl;
    27.  
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 
    outputs:
    Qt Code:
    1. 01397253234012345671416188757INFO-1869543-185A-11C0B3-MV1R-10
    To copy to clipboard, switch view to plain text mode 
    which is exactly what you receive.

    You want something closer to this but you need to think about byte order and what QDataStream is expecting to see:
    Qt Code:
    1. #include <cstdint>
    2. #include <iostream>
    3. #include <sstream>
    4.  
    5. template <typename T>
    6. std::stringstream& put ( std::stringstream& str, const T& value )
    7. {
    8. union coercion {
    9. T value;
    10. char data[ sizeof ( T ) ];
    11. };
    12.  
    13. coercion c;
    14. c.value = value;
    15. str.write ( c.data, sizeof ( T ) );
    16. return str;
    17. }
    18.  
    19. int main(int argc, char **argv) {
    20. std::stringstream str;
    21. put(str, (uint32_t)0);
    22. put(str, (uint32_t)0x53486472);
    23. put(str, (uint32_t)0);
    24. put(str, (uint32_t)1);
    25. put(str, (uint32_t)2);
    26. put(str, (uint32_t)3);
    27. put(str, (uint32_t)4);
    28. put(str, (uint32_t)5);
    29. put(str, (uint32_t)6);
    30. put(str, (uint32_t)7);
    31. put(str, (uint32_t)0x54695355);
    32. str << (char*)"INFO-1"
    33. << (char*)"869543-1"
    34. << (char*)"85A-1";
    35. put(str, (uint16_t)1);
    36. str << (char*)"C0B3-MV1R-1";
    37. put(str, false);
    38.  
    39. return 0;
    40. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 14th February 2018 at 05:33.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. Replies: 7
    Last Post: 5th December 2016, 12:57
  2. mixing qt and mfc
    By marco.stanzani in forum Qt Programming
    Replies: 4
    Last Post: 30th December 2012, 18:30
  3. weird weird mingw32-make
    By KillGabio in forum Qt Programming
    Replies: 19
    Last Post: 20th January 2012, 21:09
  4. Mixing MFC and Qt
    By Lele in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2006, 15:55
  5. Mixing C and Qt
    By forloRn_ in forum Qt Programming
    Replies: 6
    Last Post: 21st February 2006, 01:56

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.