Transfered file is modified!!!



  • hi All,
    I tryed to ransfer a file using QTcpSocket and QTcpServer and i succeded to do that.But, I have just a little problem.It is that the received file is modified in its content. Following the reception, when I open the file, I found a line added to the beginning of the file, indicating the path to access the file from the sender (who sent file), how can I fix it not to add this line to the file in the reception??
    client:
    @void FenClient::on_boutonEnvoyer_2_clicked()
    {
    QString nomFichier = lineEdit->text();
    QFile file(nomFichier);
    if(!file.open(QIODevice::ReadOnly))
    {
    qDebug() << "Erreur Le fichier n'a pas pu être ouvert !";
    return;
    listeMessages->append("Erreur Le fichier n'a pas pu être ouvert !");
    }

        QByteArray bytes = file.readAll();  
    
        QByteArray block;  
        QDataStream out(&block, QIODevice::WriteOnly);  
    
    
        out << (quint32)0;  
       // out<<fileSize ;  
        //out << nomFichier;  
        out << bytes;  
        out.device()->seek(0);  
        out << quint32((block.size() -sizeof(quint32)));  
    
        qDebug() << "Etat : envoi en cours...";  
         listeMessages->append("Etat : envoi en cours...");  
    
    
        socket->write(block);  
        socket->flush();  
    
    
            qDebug() << "[Connex]Fichier "<<nomFichier<<" envoyé !";  
    

    }
    / On a reçu un paquet (ou un sous-paquet)
    void FenClient::donneesRecues()
    {

    QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender()); //--&gt; permet de connaitre le client qui envoi les données (son socket)  
    
      QString filename="out.ddd";  
    QFile file&#40;filename&#41;;  
        if(!(file.open(QIODevice::Append)))  
        {  
            qDebug("File cannot be opened.");  
            exit(0);  
        }  
        else  
          qDebug("File created.");  
    
        QByteArray read = socket->read(socket->bytesAvailable());  
        qDebug() << "Read    : " << read.size();  
        file.write(read);  
        file.close();  
        lineEdit_2->setText((filename));  
    

    }@
    and the server:
    @void FenServeur::donneesRecues()
    {

    QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender()); //--&gt; permet de connaitre le client qui envoi les données (son socket)  
    
      QString filename="out.ddd";  
    QFile file&#40;filename&#41;;  
        if(!(file.open(QIODevice::Append)))  
        {  
            qDebug("File cannot be opened.");  
            exit(0);  
        }  
        else  
          qDebug("File created.");  
    
        QByteArray read = socket->read(socket->bytesAvailable());  
        qDebug() << "Read    : " << read.size();  
        file.write(read);  
        file.close();  
        envoyerATous((filename));  
    

    }
    void FenServeur::envoyerATous(const QString &nomFichier)
    {

    QFile file&#40;nomFichier&#41;;  
    if(!file.open(QIODevice::ReadOnly))  
    {  
        qDebug() << "Erreur Le fichier n'a pas pu être ouvert !";  
        return;  
    
    }  
    
    //size=file.size();  
    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...";  
    
    // Envoi du paquet préparé à tous les clients connectés au serveur  
    for (int i = 0; i < clients.size(); i++)  
    {  
        clients[i]->write(block);  
    
    }  
    

    qDebug() << "[Connex]Fichier "<<nomFichier<<" envoyé !";
    } @
    Thanks


  • Moderators

    It looks a bit as you are posting 4 routines, but they are mostly identical. However, that is normal, since the transfer is between sockets. That one is initially the server does not matter at the end.
    In line 43 respectively in line 9 you are opening the files with the attribute "QIODevice::Append". That may be the reason of an extra line.
    If I understand code correctly, you are sending the size of the block first. Then you are sending the block itself. On the receiving side this is apparently not matched.



  • yes, i used the "QIODevice::Append" which will allows me to add new line without deleting the others.


Log in to reply
 

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