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. QtcpSocket: file sent is lost
Forum Updated to NodeBB v4.3 + New Features

QtcpSocket: file sent is lost

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.3k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    cerina
    wrote on 28 Sept 2012, 14:31 last edited by
    #1

    Good morning, I’m looking for an example about sending a file from one pc to an other with QTcpSocket. I tried to create my own code. I have an application, in which, the user will choose a file from his DD ( all types) and send it to the TcpServer, this server will then send this file to the other clients.But, I have a problem, when i choose the file and i send it, in the client’s side, i have this message: file is sending , but in the server’s side, it shows me that the file isn’t recieved with it’s totaly bytes.
    Any suggestion please. This is the function for sending the file in the client’s side:
    @void FenClient::on_boutonEnvoyer_2_clicked()
    {
    QString nomFichier = lineEdit->text();
    QFile file(lineEdit->text());
    if(!file.open(QIODevice::ReadOnly))
    {
    qDebug() << "Error, file can't be opened successfully !";
    return;

        }
    
        QByteArray bytes = file.readAll();
    
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
    
        out << quint32(0);
        out << nomFichier;
        out << bytes;
        out.device()->seek(0);
        out << quint32((block.size() - sizeof(quint32)));
    
        qDebug() << "Etat : envoi en cours...";
         listeMessages->append("status : sending the file...");
    
        socket->write(block);
    

    }@
    and the server side:
    @void FenServeur::datarecieved()
    {

    QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
    
        if(socket == 0)
        {
            qDebug() << "no Socket!";
            return;
        }
    
        forever
        {
            QDataStream in(socket);
            if(blockSize == 0)
            {
                if(socket->bytesAvailable()  )
                {
                    qDebug() << "Error < sizeof(quint32))";
                    return;
                }
    
                in >> blockSize;
            }
    
            if(socket->bytesAvailable() < blockSize)
             {
                qDebug() << "data not recieved with its total bytes";
    
                return;
             }
    
            qDebug() << "!!!!!!";
            QByteArray dataOut;
            QString nameFile;
            in >> nameFile >> dataOut;
            QFile fileOut(nameFile);
            fileOut.open(QIODevice::WriteOnly);
            fileOut.write(dataOut);
            fileOut.close();
    
            blockSize = 0;
         }
    

    }

    void FenServeur::sendToAll(const QString &message)
    {

    QByteArray paquet;
    QDataStream out(&paquet, QIODevice::WriteOnly);
    
    out << (quint32) 0;
    out << message;
    out.device()->seek(0);
    out << (quint32) (paquet.size() - sizeof(quint32));
    for (int i = 0; i < clients.size(); i++)
    {
        clients[i]->write(paquet);
    }
    

    }@

    Thanks

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on 28 Sept 2012, 20:31 last edited by
      #2

      Closed as a duplicate: Please go "here":https://qt-project.org/forums/viewthread/20724/ instead.

      1 Reply Last reply
      0

      1/2

      28 Sept 2012, 14:31

      • Login

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