Results 1 to 7 of 7

Thread: How to send a array to server?

  1. #1
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default How to send a array to server?

    Hi all
    I am in a big trouble , how i send a array to server.
    I have a array of "quint8" type i have to send this to server . I have no idea how to send this.
    Please help me.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to send a array to server?

    send the number of items you have followed by the items themselves

  3. #3
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: How to send a array to server?

    Thanks for reply but how to send them.
    I triy with the following code but it is not working.

    Qt Code:
    1. uint8_t temp_var;
    2. uint8_t arr[LENGTH];
    3. uint8_t var;
    4. temp_var = packet_formation(2, ON);
    5. for(int i=0; i<temp_var; i++)
    6. {
    7. arr[i] = packet_frame[i];
    8. qDebug() <<arr[i];
    9. var = arr[i];
    10. }
    11. QByteArray block;
    12. QDataStream out(&block, QIODevice::WriteOnly);
    13. out.setVersion(QDataStream::Qt_4_5);
    14. out<<uint16_t(temp_var)<<uint8_t('S')<<arr;
    15. qDebug() << "Message"<<out<<uint16_t(temp_var)<<uint8_t('S')<<arr;
    16. out.device()->seek(0);
    17. out << quint16(block.size() - sizeof(quint16));
    18. socket->write(block);
    To copy to clipboard, switch view to plain text mode 


    where uint8_t is unsigned char.

  4. #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: How to send a array to server?

    What about it doesn't work? Your code compiles and sends something. Only you know what part of the behaviour is not what you expected.

    At a guess... Streaming the pointer "arr" is not doing what you think; my compiler generates a useful warning:
    Qt Code:
    1. main.cpp:20: warning: the address of ‘arr’ will always evaluate as ‘true’
    To copy to clipboard, switch view to plain text mode 
    and yours probably does also. Line 14 is sending a 16-bit integer, an 8-bit integer, and another single 8-bit integer representing the boolean value true. (Use QByteArray::toHex() to see exactly what is in the buffer) You are expecting it will send the content of the array and there is no way for that to happen.

    As Squidge said, you need to send the number of items in the array followed by the array items (one at a time). Your code is not doing that.

  5. #5
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: How to send a array to server?

    i tried it by sending the no of items (By temp_var) .
    Can you please be more ellobarated , give some code sample so i can understand.

    What it send in my case it is not sending nothing(in my server i am getting nothing)
    Thankyou.

  6. #6
    Join Date
    Jul 2011
    Location
    Brasil
    Posts
    39
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send a array to server?

    Hello,

    I'm not sure, but you can try:
    Qt Code:
    1. socket->write((const char *)arr, temp_var*sizeof(uint8_t)); //if temp var have the size...
    To copy to clipboard, switch view to plain text mode 

    (don't know why write() takes const char * and not a void *...)

    Try allocating arr dynamically (malloc or new) after knowing the number of elements with 2 more room and in your for do this:

    Qt Code:
    1. arr[i+2] = packet_frame[i];
    To copy to clipboard, switch view to plain text mode 

    so you can
    Qt Code:
    1. arr[0]=temp_var;
    2. arr[1]='S';
    To copy to clipboard, switch view to plain text mode 
    outside the for...

    In my app I've done everything memcpying into a malloc'ed char*...

    (Sorry my English mistakes... I'm still learning )

    HTH

  7. #7
    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: How to send a array to server?

    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3. #include <stdint.h>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication app(argc, argv);
    8.  
    9. const int size = 10;
    10.  
    11. // Some test data
    12. uint8_t arr[size];
    13. for (int i=0; i<size; ++i)
    14. arr[i] = i;
    15.  
    16.  
    17. QByteArray block;
    18. QDataStream out(&block, QIODevice::WriteOnly);
    19. out.setVersion(QDataStream::Qt_4_5);
    20. out << uint32_t(size); // number of elements, allows for 4 billion elements
    21. for (int i=0; i<size; ++i)
    22. out << arr[i];
    23.  
    24. qDebug() << block.toHex();
    25. // then write the data to the socket
    26. return 0;
    27. }
    To copy to clipboard, switch view to plain text mode 


    If there are no bytes reaching the other end then the problem is elsewhere. Is your socket connected?

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

    Niamita (25th July 2011)

Similar Threads

  1. How to send msg on button click to server.
    By Niamita in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2011, 08:59
  2. qml send a list/array from signal
    By goli in forum Qt Programming
    Replies: 0
    Last Post: 9th May 2011, 09:46
  3. send request via ISA PROXY SERVER
    By rmagro in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2010, 22:56
  4. Http server : send images
    By fitzy in forum Qt Programming
    Replies: 1
    Last Post: 2nd November 2009, 12:04
  5. Connect to a server, send data and exit
    By Pepe in forum Newbie
    Replies: 6
    Last Post: 27th July 2007, 11:29

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.