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. send a struct via UDP (Without serialization)
Forum Updated to NodeBB v4.3 + New Features

send a struct via UDP (Without serialization)

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.5k Views 2 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.
  • PuntP Offline
    PuntP Offline
    Punt
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @Punt said:

      QDataStream

      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. :)

      1 Reply Last reply
      1
      • kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @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.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        2

        • Login

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