My server can't recieve the file sent by the client with its totaly size using Qtcpsocket



  • 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:
    @#include "FenServeur.h"

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.