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.4k 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.
  • P Offline
    P Offline
    Punt
    wrote on 9 Jun 2016, 07:14 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 9 Jun 2016, 08:38 last edited by mrjj 6 Sept 2016, 08:47
      #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
      • K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 9 Jun 2016, 11:42 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

        1/3

        9 Jun 2016, 07:14

        • Login

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