Results 1 to 3 of 3

Thread: how to limit char * arrays?

  1. #1
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default how to limit char * arrays?

    I noticed both the QUdpSocket::readDatagram() and QUdpSocket::writeDatagram() functions accept char *'s as parameters.

    On the readDatagram side you're supposed to pass in a length parameter, where I've been putting a constant: 1024.

    But when I send char *'s and then receive them, I get these trailing characters. For example when I send: "CONTAIN_STRANGEMATERIALFACILITY"

    I get: "CONTAIN_STRANGEMATERIALFACILITY¿äî"

    I've tried making the last character '0' and NULL, and newline (10), but they don't work.

    My server code:
    Qt Code:
    1. QHostAddress the_address;
    2. quint16 the_port = 0;
    3. char char_incoming[1024];
    4.  
    5. readDatagram( (char *)char_incoming, 1024, &the_address, &the_port );
    6. cout << "incoming string is: " << char_incoming << endl;
    To copy to clipboard, switch view to plain text mode 

    And my client code:
    Qt Code:
    1. QString request_string( "CONTAIN_STRANGEMATERIALFACILITY" );
    2. char buffer[512];
    3. int buffer_length = snprintf( buffer, sizeof buffer, request_string.toAscii().data() );
    4. cout << "buffer_length = " << buffer_length << endl;
    5.  
    6. //buffer[ buffer_length ] = 10; // tried this as well, just puts in a newline
    7. //buffer_length++;
    8.  
    9. int success = writeDatagram( buffer, host_address, port_number );
    To copy to clipboard, switch view to plain text mode 

    And then on the server side I get a bunch of junk.

    I'm not sure how I can know the size before I read it.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to limit char * arrays?

    readDatagram() returns the number of bytes actually read.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    mhoover (24th June 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to limit char * arrays?

    Ah. I should have thought of that.

    Thanks again, wysota.

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 20:48
  2. char * problem
    By eleanor in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2008, 14:06
  3. char arrays to "multiline editor"
    By baray98 in forum Qt Programming
    Replies: 2
    Last Post: 29th January 2008, 06:19
  4. ActiveQt 2-dim arrays
    By nile.one in forum Qt Programming
    Replies: 1
    Last Post: 17th October 2007, 04:26
  5. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 20:49

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
  •  
Qt is a trademark of The Qt Company.