Image transfer through QTCPSocket from client to server ..
Unsolved
General and Desktop
-
I'm trying to send an image file from client pc to server pc,which are connected to ethernet port. I was able to connect client and server and send image data from client , but the server is not receiving the image data. My goal is to display the image sent from client on the server GUI.
server code:
server.cpp:
#include "server.h" Server::Server(QObject *parent) : QTcpServer(parent),imageSize(0) { listen(QHostAddress::Any, 1234); // Listen on port 1234 } void Server::incomingConnection(qintptr socketDescriptor) { QTcpSocket *socket = new QTcpSocket(this); socket->setSocketDescriptor(socketDescriptor); emit clientConnected(socket->peerAddress().toString()); connect(socket, &QTcpSocket::readyRead, this, &Server::readyRead); } void Server::readyRead() { QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender()); if (!socket) { return; } socket->read(reinterpret_cast<char*>(&imageSize), sizeof(qint64)); imageData.append(socket->read(imageSize)); QImage image; image.loadFromData(imageData, "JPG"); emit imageReceived(image); imageData.clear(); imageSize = 0; }
main.cpp:
int main(int argc, char *argv[]) { QApplication a(argc, argv); QLabel label; label.show(); label.setFixedSize(800, 600); Server server; QObject::connect(&server, &Server::imageReceived, [&](const QImage &image){ label.setPixmap(QPixmap::fromImage(image)); }); QObject::connect(&server, &Server::clientConnected, [&](const QString &clientAddress){ qDebug() << "Client connected:" << clientAddress; }); return a.exec(); }
client code:
client.cpp:
#include "client.h" #include <QDebug> Client::Client(QObject *parent) : QObject(parent) { socket = new QTcpSocket(this); connect(socket, &QTcpSocket::connected, this, &Client::connected); connect(socket, &QTcpSocket::bytesWritten, this, &Client::bytesWritten); connect(socket, &QTcpSocket::disconnected, this, &Client::disconnected); } void Client::sendImage(const QString& imagePath) { file.setFileName(imagePath); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "Could not open file"<< file.errorString(); return; } socket->connectToHost("192.168.1.2", 1234); // Server IP and port // Close the connection when all data has been written connect(socket, &QTcpSocket::bytesWritten, this, &Client::checkBytesWritten); connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater); // file.close(); } void Client::connected() { qDebug() << "Connected to server"; // Get the size of the file qint64 imageSize = file.size(); // Start sending image data QByteArray imageData = file.readAll(); socket->write(imageData); file.close(); } void Client::bytesWritten(qint64 bytes) { qDebug() << "Bytes written: " << bytes; } void Client::disconnected() { qDebug() << "Disconnected from server"; } void Client::error(QAbstractSocket::SocketError error) { qDebug() << "Error: " << error; } void Client::readyRead() { qDebug() << "Received acknowledgment from server: " << socket->readAll(); // Close the connection after receiving acknowledgment socket->close(); } void Client::checkBytesWritten(qint64 bytes) { if (bytes == 0) { // All data has been written, close the connection socket->disconnectFromHost(); } }
main.cpp:
#include "client.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Client client; QString imagePath = "C:/Users/Intel/Downloads/client/moon.jpg"; // Send the image file to the server client.sendImage(imagePath); return a.exec(); }
-
@Prakash08 You seem to expect that the whole image will be available at once - this is not how TCP works - you will get the image data in chinks, so the readyRead slot will be called several times.
"but the server is not receiving the image data" - did you do any debugging to see which part is not working? Is the readyRead slot called?