Results 1 to 4 of 4

Thread: threads and socket

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

    Default threads and socket

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: threads and socket

    You must check in what format the sender sends those floats. Are they encoded in binary form?

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

    Default Re: threads and socket

    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();

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: threads and socket

    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.

Similar Threads

  1. Replies: 10
    Last Post: 20th March 2007, 22:19
  2. signals/slots across threads
    By high_flyer in forum Qt Programming
    Replies: 9
    Last Post: 12th March 2007, 23:56
  3. question about socket and threads?
    By oob2 in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2007, 11:42
  4. Thread(s) and socket, timer slots
    By stephdev1965 in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2006, 14:04
  5. Organizing multiple threads
    By vratojr in forum Qt Programming
    Replies: 3
    Last Post: 25th May 2006, 09:53

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.