Aarom
Aarom

Reputation: 13

Image file transfer through Ethernet Port from client to server

I'm trying to send an image file from client pc to server pc,which are connected to ethernet port. And I have set the ip manually to 192.168.1.2(server) and 192.168.1.3 (client) with subnet mask to 255.255.255.0 on both PC's. 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.

thanks in advance !

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

Upvotes: 0

Views: 54

Answers (1)

SRedouane
SRedouane

Reputation: 508

On the recieving side you read the image size then the image itself, but on the sending side you send only the image. you should do something like this:

   QByteArray imageData = file.readAll(); 
    qint64 imageSize = imageData.size();
// Start sending image size
    socket->write(&imageSize,sizeof(qint64));
// Start sending image data
    socket->write(imageData);

Upvotes: 0

Related Questions