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.
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.
send the number of items you have followed by the items themselves
Thanks for reply but how to send them.
I triy with the following code but it is not working.
Qt Code:
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; 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);To copy to clipboard, switch view to plain text mode
where uint8_t is unsigned char.
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: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.Qt Code:
main.cpp:20: warning: the address of ‘arr’ will always evaluate as ‘true’To copy to clipboard, switch view to plain text mode
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.
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.
Hello,
I'm not sure, but you can try:
Qt Code:
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:
arr[i+2] = packet_frame[i];To copy to clipboard, switch view to plain text mode
so you can
outside the for...Qt Code:
arr[0]=temp_var; arr[1]='S';To copy to clipboard, switch view to plain text mode
In my app I've done everything memcpying into a malloc'ed char*...
(Sorry my English mistakes... I'm still learning)
HTH
Qt Code:
#include <QtCore> #include <QDebug> #include <stdint.h> int main(int argc, char *argv[]) { const int size = 10; // Some test data uint8_t arr[size]; for (int i=0; i<size; ++i) arr[i] = i; QByteArray block; 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; }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?
Niamita (25th July 2011)
Bookmarks