Results 1 to 6 of 6

Thread: sending int array through socket

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Question sending int array through socket

    hi

    i have an int array which should be send through socket .
    the problem is if i store the array in QByteArray(using setNum() and number() it takes one byte for every digit.

    but i want to store any integer value in QByteArray as 4 bytes.

    ie . each integer byte should be stored as a character in QByteArray

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending int array through socket

    Are you sure QByteArray::number works like that?
    If so, then maybe you will consider using my solution from your other post... It does just that - appends 4 bytes to the byte array( for 32 bit architectures, for 64 bit will append 8 bytes).
    Last edited by marcel; 20th December 2007 at 07:39.

  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: sending int array through socket

    ya i'm sure .
    for ex:
    Qt Code:
    1. int i = 24558;
    2. arr = arr.setNum(i);
    3. qDebug()<<arr.size();
    To copy to clipboard, switch view to plain text mode 

    the output is 5


    ex2:

    Qt Code:
    1. int i = 245;
    2. arr = arr.setNum(i);
    3. qDebug()<<arr.size();
    To copy to clipboard, switch view to plain text mode 

    the output is 3

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: sending int array through socket

    Yes, that's what QByteArray::setNum() says it does. You might want to take a loot at QDataStream.
    J-P Nurmi

  5. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: sending int array through socket

    hi jpn
    actually i wnt an int arrray to send through the socket.

    how to output the QDataStream to console
    the following does not works

    Qt Code:
    1. QDataStream stream;
    2. int i=63;
    3. stream <<i;
    4. qDebug()<<stream;
    To copy to clipboard, switch view to plain text mode 


    is it possible to convert a QDataStream to QByteArray.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: sending int array through socket

    Open a data stream on the socket, just like many of the network examples do.
    J-P Nurmi

Similar Threads

  1. question about socket and threads?
    By oob2 in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2007, 11:42
  2. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 09:23

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.