Results 1 to 8 of 8

Thread: Transfer image problem

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Transfer image problem

    I have two computers. They are able to send text messages to each other, but as soon as I want to pass an image the program crashes.

    To read the sent data I am using "readAll();"

    How do I solve this ?

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

    Default Re: Transfer image problem

    Quote Originally Posted by probine
    How do I solve this ?
    Use a debugger and try to obtain the stack trace --- it will show you where your program crashed.

    If you have gdb, try this:
    $ gdb ./application
    (gdb) run
    <program starts and crashes>
    (gdb) bt
    <backtrace>
    (gdb) quit
    (just make sure that you have compiled your program in debug mode).

    If you use windows make sure you have "CONFIG += console" in the .pro file, so that you can see all messages from Qt.

  3. #3
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Transfer image problem

    You are suggesting that the problem is somewhere else then.

    This is what happens:

    1. When the 2 clients (program) are running in the same machine as the server, then the image is passed successfully.

    2. When I run the server and 1 client in Linux, and the other client in Windows, then the program crashes.

    When I pass regular text messages, then everything is fine, because the messages are small. I have a "cout<<" in the server that prints IMAGE everytime the payload is greater than 500.

    readAll(); is executed everytime a payload arrives, so in the screen I see many IMAGE messages, because the image is large.

    I thought that readAll(); will take care of it, but I may be wrong.

    Qt Code:
    1. // Creates the needed connections
    2. void ClientThread :: iniConnections()
    3. {
    4. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
    5. connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
    6. }
    7.  
    8.  
    9. // Reads all incoming data and passes it to the message class
    10. void ClientThread :: readData()
    11. {
    12. messageIn->clear();
    13. *messageIn = tcpSocket->readAll();
    14. if(messageIn->length() >= 500)
    15. {
    16. cout << "SCREENSHOT\n";
    17. remoteClientThread->directScreenshot(*messageIn);
    18. }
    19. else
    20. message->parseMessage(*messageIn);
    21. }
    To copy to clipboard, switch view to plain text mode 

    I know that the problem is in this readAll(); because the image is passed in pieces and the program cannot display those pieces.

    How can I collect all the image coming from the remote computer in my QByteArray before passing the image to the label ?

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

    Default Re: Transfer image problem

    Quote Originally Posted by probine
    How can I collect all the image coming from the remote computer in my QByteArray before passing the image to the label ?
    You need something like this:
    Qt Code:
    1. void ClientThread :: readData()
    2. {
    3. buffer.append( tcpSocket->readAll() );
    4. int messageSize = 0;
    5. do {
    6. messageSize = parseMessage( buffer );
    7. buffer.remove( 0, messageSize );
    8. }
    9. while( messageSize > 0 );
    10. }
    To copy to clipboard, switch view to plain text mode 
    There are three possibilities:
    • the buffer contains only a part of a message (in this case parseMessage() should only return 0),
    • the buffer contains a message followed by a part of another message (in this case parseMessage() should parse that message and return its size),
    • the buffer contains an invalid message (it might happen due some communication errors or bugs on the other side).

  5. #5
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Transfer image problem

    I understand what you are saying... thanks fot answering.

    The problem I have is that the QByteArray is passed to the respective function to be treated before I was able to store all the data in it.

    Example:

    A client sends an image. The size of the image is variable, but in this case lets say 5000 bytes.

    The server starts receiving the image, but it only received 1250, so the server should wait and append the rest of the image to the QByteArray.

    Is there a function in QTcpSocket that tells me the total size of the package ??? in this case will be 5000.

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

    Default Re: Transfer image problem

    Quote Originally Posted by probine
    Is there a function in QTcpSocket that tells me the total size of the package ??? in this case will be 5000.
    No, with TCP you receive just a stream of bytes --- it's your job to determine where the message ends. If you were using QDataStream, Qt could help you a bit with this.

  7. #7
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Transfer image problem

    How could I determine where the package ends ?

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Transfer image problem

    Pass the length of the image as the first field of your message, and/or define an end mark which you always append to the end of the message.
    J-P Nurmi

Similar Threads

  1. Help needed handling image data
    By toratora in forum General Programming
    Replies: 2
    Last Post: 11th May 2007, 09:24
  2. how i can add image in my toolbar
    By jyoti in forum Qt Tools
    Replies: 7
    Last Post: 19th December 2006, 14:39
  3. problem with the back ground image
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2006, 21:34
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. problem with image colections
    By zlatko in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2006, 13:43

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.