PDA

View Full Version : Image transfer using QTcpsocket



anmol2701
16th June 2015, 07:07
Hello Experts!

Can anybody guide how to transfer image over QTcpsocket.

I am able to send image using code:
QByteArray read ;
inputFile.open(QIODevice::ReadOnly);
while(1)
{
read.clear();
read = inputFile.read(inputFile.size());
qDebug() << "Read : " << read.size();
if(read.size()==0)
break;
qDebug() << "Written : " << socket->write(read);
socket->waitForBytesWritten();
read.clear();
}

But how to read and save image on client side??

jefftee
16th June 2015, 07:16
Are your images available via http?

Typically you would use QNetworkAccessManager, QNetworkRequest, and QNetworkReply to perform a QNetworkAccessManager::get request to retrieve an image over the network.

Unless you have a specialized server listening for network requests that receive an image filename and send the binary contents to the client, I suspect what you want is to issue the get request as described above.

anmol2701
16th June 2015, 07:22
Thanks for your reply..

I am working client server app..
Chat is working properly even i am able transfer text file..
But facing problem with image..

jefftee
16th June 2015, 07:48
So if you want to write your own server that sends images to clients, you'll need to do the following:

For the server:


Use QTcpServer and listen to a known TCP/IP port number greater than 1024
Read the image filename sent by the client and verify it exists
Open the image file, read the binary data and send to the client
Close the connection to the client once the data is sent


For the client:


Use QTcpSocket and connect to the server via ip addr or hostname and known port that server is listening to
Send the image filename you want to receive to the server
Read data from the socket and save to local file
Close the file once the server closes the connection


Since TCP/IP is stream oriented, you will typically precede each request with some metadata about the data that follows so that you can determine when you have received a complete request. For example, I'd precede the actual image data with the length of the image file, so you can verify you receive the full image file, etc.

If you don't know how to get started, you have a challenge ahead of you. Google around for some Qt TCP/IP client/server examples and study the examples provided by Qt as well.

Good luck.

Leite
18th June 2015, 15:06
It can help you : http://hanckmann.com/2010/12/05/blog.html