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. Transfered file is modified!!!
Forum Updated to NodeBB v4.3 + New Features

Transfered file is modified!!!

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.4k 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 last edited by
    #1

    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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cerina
        wrote on last edited by
        #3

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

        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