PDA

View Full Version : threads and socket



babu198649
10th April 2008, 12:41
hi
i have an array of size[n](float data type) and i want to fill the array with the data collected from the socket in one thread and in the other thread i have to use the array(while it is getting filled with the other thread) for gui related purpose(plotting graphs).

i am not able to find a solution with QDataStream.the blocking fortune client example is different from my program.

if the array consists of char i can use the bool QIODevice::getChar ( char * c )
to get the single data from the socket but i have floats in the array
so ,what functions should i use to get the float values serially from the socket.

thank u

jacek
10th April 2008, 15:01
You must check in what format the sender sends those floats. Are they encoded in binary form?

babu198649
10th April 2008, 15:18
the format of the data is such that the first 2 bytes tells the total size(in bytes) of the data (similar to the fortune server example).

i am the sender and hence this is the code used to send the data(ie.server)


QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out << (quint16)0;

#val_var_size is the size of the array and val_var is the floating pointer
for (long i=0; i<val_var_size; i++)
out<<*(val_var+i);

out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

QTcpSocket *socket = server->nextPendingConnection();
connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
socket->write(block);
socket->disconnectFromHost();

jacek
10th April 2008, 16:34
If you write the data using QDataStream, then you should read it also using QDataStream, but I'm not sure about that block size. I think it would be easier to send the number of items in the array, not the size. And don't forget that the data might be split into several packets.