Results 1 to 4 of 4

Thread: Reading UDP data properly

  1. #1
    Join Date
    Jan 2016
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Reading UDP data properly

    Hi all,

    I am trying to read back the udp data , sent from my stepper motor. Reply varies from 3-12 bytes.

    I am currently doing this to read my udp data:

    Qt Code:
    1. qint64 size = udpSocket->pendingDatagramSize();
    2. QHostAddress sender;
    3. quint16 senderPort;
    4. char* reply = new char[size]
    5. qint64 res = udpSocket->readDatagram( reply, size, &sender, &senderPort );
    6.  
    7. char buf[12];
    8. for ( int i = 2; i <= 11; ++i )
    9. buf[ i -2 ] = reply[ i ];
    10.  
    11. ui->Position->setText(buf);
    To copy to clipboard, switch view to plain text mode 

    The problem I am facing is that sometime there are only 4 bytes sometime there are 11 bytes and when there are less bytes suppose 4-5 my text box will be filled with weird characters of unreadable format.
    how can I change my current code to just read proper bytes received.

  2. #2
    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: Reading UDP data properly

    If you persist using basic char[] then you are responsible for ensuring that the buffer is Nul terminated '\0' before trying to use the buffer as a C string. There is also no guarantee that the bytes of the datagram are printable characters in the first place.

    Currently your datagram buffer is a memory leak. If you use QByteArray exactly as in the QUdpSocket docs then you will have neither the memory leak nor nul termination issues.

  3. The following user says thank you to ChrisW67 for this useful post:

    ankit.1g@gmail.com (15th February 2016)

  4. #3
    Join Date
    Jan 2016
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading UDP data properly

    Quote Originally Posted by ChrisW67 View Post
    If you persist using basic char[] then you are responsible for ensuring that the buffer is Nul terminated '\0' before trying to use the buffer as a C string. There is also no guarantee that the bytes of the datagram are printable characters in the first place.

    Currently your datagram buffer is a memory leak. If you use QByteArray exactly as in the QUdpSocket docs then you will have neither the memory leak nor nul termination issues.
    thank you very much, but how can I change my code to use QByteArray? should i change char* reply to QByteArray reply?


    Added after 5 minutes:


    I get Error " conversion to QByteArray to char* is ambiguous :

    qint64 size = udpSocket->pendingDatagramSize();
    QByteArray datagram;
    datagram.resize(size);
    udpSocket->readDatagram( datagram, size, &sender, &senderPort );
    Last edited by ankit.1g@gmail.com; 15th February 2016 at 20:05.

  5. #4
    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: Reading UDP data properly

    At the risk of sounding like a broken record...
    Quote Originally Posted by Me
    If you use QByteArray exactly as in the QUdpSocket docs then you will have neither the memory leak nor nul termnation issues.

Similar Threads

  1. Replies: 0
    Last Post: 12th June 2014, 12:37
  2. reading hex data from QFile
    By Charvi in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2012, 11:54
  3. File reading does not output properly
    By sydjung in forum Newbie
    Replies: 10
    Last Post: 14th October 2011, 10:04
  4. Replies: 1
    Last Post: 14th June 2011, 15:50
  5. Reading binary data
    By vermarajeev in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 09:14

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.