Results 1 to 6 of 6

Thread: Issue using QTcpSocket - works for QString but not for QImage

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2009
    Location
    London
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Issue using QTcpSocket - works for QString but not for QImage

    Hi,

    I have written a server/client, initially using the Fortune examples. The data received is a QVariant - so the QVariant type can be used to determine what to do with the data.

    Apologies if my explanations are not great - I am not great with describing what I want to do/have done in programming-speak.

    For e.g. a QString it works fine (I guess as it comes in one block?). however attempting with a QImage -> the version/description/data type are sent OK --> If I output the emit_message in read_client then it outputs these OK. I assume this is because the images are larger and need to be sent in multiple blocks. Hence I believe the readClient() would be called multiple times for the same serialized data item. I can't work out how to deal with this gracefully - should I create some function to deal with whether this is a fresh readClient() or one to deal with data that is mid-transmission?
    Either that, or I need some sort of loop based on data/the size sent in the header, to build up "data" from the blocks/packets received but am unsure of how to go about doing this. I also send an end of data flag - could this also be used? I have looked through the forums - there are multiple issues on this topic, but I could not find a specific answer (well not with the search terms I have been using)...

    Any help would be much appreciated (as well as any pointers if my code is poor -> I am a bit of a QT noob)..

    Here is a stripped down version of the codes.

    Server:
    Qt Code:
    1. void Server::incomingConnection(int socketId) {
    2. ServerSocket *socket = new ServerSocket(this);
    3. socket->setSocketDescriptor(socketId);
    4. }
    5.  
    6.  
    7. ServerSocket::ServerSocket(QObject *parent) : QTcpSocket(parent)
    8. {
    9. connect(this, SIGNAL(readyRead()), this, SLOT(readClient()));
    10. connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
    11. blockSize = 0;
    12. }
    13.  
    14. void ServerSocket::readClient()
    15. {
    16. QDataStream in(this);
    17. in.setVersion(QDataStream::Qt_4_5);
    18. if (blockSize == 0) {
    19. if (bytesAvailable() < (sizeof(quint16)))
    20. return;
    21. in >> blockSize;
    22. }
    23. if (bytesAvailable() < (quint16)(blockSize + sizeof(quint16) + sizeof(quint8)))
    24. return;
    25. quint8 version;
    26. QString description;
    27. QVariant data;
    28. quint16 endFlag;
    29. in >> version;
    30. in >> description;
    31. in >> data;
    32. in >> endFlag;
    33.  
    34. QString emit_message = "\n\tVersion: " + QString::number(version) +
    35. "\n\tType(" + QString(data.typeName()) + "): " + QString::number(data.type()) +
    36. "\n\tSize: " + QString::number(blockSize) +
    37. "\n\tMessage: " + description +
    38. "\n\tData: " + data.toString() +
    39. "\n\tEndFlag: " + QString::number(endFlag);
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 


    client:
    Qt Code:
    1. //this is fired on the click of a QPushButton
    2. void Client::sendRequest()
    3. {
    4. QTcpSocket socket;
    5. socket.connectToHost(serverName, serverPort);
    6.  
    7. if (!socket.waitForConnected(TIMEOUT)) {
    8. emit error(socket.error(), socket.errorString());
    9. return;
    10. }
    11.  
    12. QVariant data;
    13. QString description;
    14. //text is set in a QLineEdit
    15. if (text == "image") {
    16. data = QImage("someimage.jpg");
    17. description = "This is an image";
    18. }
    19. else {
    20. data = text;
    21. description = "This is some text";
    22. }
    23.  
    24. QByteArray block;
    25. QDataStream out(&block, QIODevice::WriteOnly);
    26. out.setVersion(QDataStream::Qt_4_5);
    27. out << quint16(0) << quint8(1) << description << data << quint16(0xFFFF);
    28. out.device()->seek(0);
    29. out << (quint16)(block.size() - sizeof(quint16) - sizeof(quint8) - sizeof(quint16));
    30. socket.write(block);
    31. socket.flush();
    32. qDebug() << "Sending: [" + description + data.toString()
    33. + "] to server(" + serverName + ":" + QString::number(serverPort) + ")";
    34. }
    To copy to clipboard, switch view to plain text mode 

    Thanks,

    James

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Issue using QTcpSocket - works for QString but not for QImage

    What do you send as the block size?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2009
    Location
    London
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Issue using QTcpSocket - works for QString but not for QImage

    Hi Wysota,

    Is it not (line 27 of the client code):
    Qt Code:
    1. out << (quint16)(block.size() - sizeof(quint16) - sizeof(quint8) - sizeof(quint16));
    To copy to clipboard, switch view to plain text mode 
    ?

    Which is the size of everything minus itself(quint16), the version(quint8) and the endFlag(quint16). I have tried casting it to a quint64 and 32 instead but this makes no difference. Although that was a few days ago so i will try this again now.

    Thanks,

    James

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Issue using QTcpSocket - works for QString but not for QImage

    What is "everything"? blocksize determines how much data needs to be available before the reading function tries to read the next request from the stream. Furthermore you are doing some complicated operations regarding filling the data stream with proper information, writing some 0, then some 1 and then trying to overwrite them with something else (the block size, I guess).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. how to use qtcpsocket send qimage data
    By tsuibin in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2012, 14:51
  2. QTcpSocket only works local
    By BrainStorm in forum Qt Programming
    Replies: 21
    Last Post: 4th June 2011, 16:49
  3. QImage to QString
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 7th October 2008, 07:29
  4. QImage Format Issue
    By vishal.chauhan in forum Qt Programming
    Replies: 7
    Last Post: 9th March 2007, 10:14
  5. QImage Issue
    By vishal.chauhan in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2007, 04:29

Tags for this Thread

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.