Important: Please read the Qt Code of Conduct -

send a struct via UDP (Without serialization)

  • Hi,

    I have this struct :

    typedef struct myStruct
           int nb_trame;
           bool tabBool[20];
        } myStruct;

    How do I send it via a QUdpSocket (without QDataStream) ?

    I thought this will work :

    myStruct a;
    //set 'a' variable
    mySocket->writeDatagram( (const char*) &a, sizeof(a), someQHostAddress, somePort);

    But some people told me it won't..

    If possible, i would like to have a code answer x) Because I'm a noob in networking...
    I'm using Visual C++ 2010.

  • Lifetime Qt Champion

    @Punt said:


    Is there a reason you will not use QDataStream ?

    It helps you with network details like byte order etc.

    What you are doing is very likely to blow up hard
    if u send to other pc ( that has different architecture)
    and just superimpose(cast) a myStruct over the received data.

    Normally you would use serialization. :)

  • Moderators

    @Punt said:

    But some people told me it won't..

    It will for some cases. If endianness changes between sending and reception (e.g. if you send data from windows to OSX) you'll get nonsense. Serialize your data as @mrjj said.

Log in to reply