PDA

View Full Version : How to send a array to server?



Niamita
23rd July 2011, 11:48
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.

squidge
23rd July 2011, 13:22
send the number of items you have followed by the items themselves

Niamita
25th July 2011, 05:38
Thanks for reply but how to send them.
I triy with the following code but it is not working.


uint8_t temp_var;
uint8_t arr[LENGTH];
uint8_t var;
temp_var = packet_formation(2, ON);
for(int i=0; i<temp_var; i++)
{
arr[i] = packet_frame[i];
qDebug() <<arr[i];
var = arr[i];
}
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_5);
out<<uint16_t(temp_var)<<uint8_t('S')<<arr;
qDebug() << "Message"<<out<<uint16_t(temp_var)<<uint8_t('S')<<arr;
out.device()->seek(0);
out << quint16(block.size() - sizeof(quint16));
socket->write(block);


where uint8_t is unsigned char.

ChrisW67
25th July 2011, 06:32
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:
main.cpp:20: warning: the address of ‘arr’ will always evaluate as ‘true’ 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.

Niamita
25th July 2011, 06:42
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.

NullPointer
25th July 2011, 06:54
Hello,

I'm not sure, but you can try:


socket->write((const char *)arr, temp_var*sizeof(uint8_t)); //if temp var have the size...


(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:



arr[i+2] = packet_frame[i];


so you can


arr[0]=temp_var;
arr[1]='S';

outside the for...

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

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

HTH

ChrisW67
25th July 2011, 07:12
#include <QtCore>
#include <QDebug>
#include <stdint.h>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

const int size = 10;

// Some test data
uint8_t arr[size];
for (int i=0; i<size; ++i)
arr[i] = i;


QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_5);
out << uint32_t(size); // number of elements, allows for 4 billion elements
for (int i=0; i<size; ++i)
out << arr[i];

qDebug() << block.toHex();
// then write the data to the socket
return 0;
}



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