Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Image transfer through QTCPSocket from client to server ..

Image transfer through QTCPSocket from client to server ..

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 440 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Prakash08
    wrote on last edited by Prakash08
    #1

    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();
    }
    
    jsulmJ 1 Reply Last reply
    0
    • P Prakash08

      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();
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      2
      • jsulmJ jsulm

        @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?

        P Offline
        P Offline
        Prakash08
        wrote on last edited by
        #3

        @jsulm thanks for the info and i have solved the issue. can you please help me resolve issue with my new post also.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved