Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Support on writeDatagram QUdpSocket function
Qt 6.11 is out! See what's new in the release blog

Support on writeDatagram QUdpSocket function

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 4 Posters 2.6k 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.
  • N Offline
    N Offline
    NarutoKun
    wrote on last edited by
    #1

    Hello All,

    I am trying to write UDP message to the socket.

    QUdpSocket Msg_socket;
    Msg_socket->writeDatagram(const char *data, qint64 size, const QHostAddress &address, quint16 port);

    Here writeDatagram function support const char *data, and I have my UDP message in the form of an array of unsigned char * .

    If I change my array from unsigned char * to char * in order to use writeDatagram function I see overflowing of my data to the negative range.

    Would like to know

    1. If there is any other function in QT which would support in writing my data to socket apart from the one mentioned above ?
    2. If you have any suggestion on how to handle this situation .

    Thank you for your time.

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

      Hi
      Try the ByteArray version?
      http://doc.qt.io/qt-5/qudpsocket.html#writeDatagram-1

      1 Reply Last reply
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        QUdpSocket is a QIODevice so the usual QDatastream method is the preferred one. see http://doc.qt.io/qt-5/qdatastream.html#details

        @NarutoKun said in Support on writeDatagram QUdpSocket function:

        If I change my array from unsigned char * to char * in order to use writeDatagram function I see overflowing of my data to the negative range.

        I guess you are using a reinterpret_cast<char *> so what's the problem if it goes to negative numbers?! just use reinterpret_cast<unsigned char *> to get back the original.

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        4
        • McLionM Offline
          McLionM Offline
          McLion
          wrote on last edited by
          #4

          Using this with no problems:

          QByteArray datagram;
          cmd_socket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::LocalHost, CMD_SOCK_PORT);
          
          1 Reply Last reply
          1
          • N Offline
            N Offline
            NarutoKun
            wrote on last edited by
            #5

            Re: Support on writeDatagram QUdpSocket function

            @mrjj , @VRonin & @McLion

            Thank you all for your inputs. All your inputs helped me get through my problem.

            This is what I came up with and now I am able to transmit my data correctly and observing the right behavior.

            QUdpSocket socketwr;

            = {xx, xx xx ......... };

            :
            :
            :
            :
            QByteArray data_to_socket = QByteArray::fromRawData(pack_data, pack_data_len);
            socketwr->writeDatagram(data_to_socket, *receiver_ip_add, receiver_port_add);

            Thanks again!!!
            Good Day.

            1 Reply Last reply
            1

            • Login

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