File transfer with QtcpSocket



  • Hello,
    I transfer a file using QtcpSocket in client/server application. But, the recieved file is mosdified: a line in the begin is added. For example, if the client send this file: C:/users/administrator/desktop/file.txt , the server recieved the file.txt which is modified by addind the C:/users/administrator/desktop/file.txt in its beginning when it was sent. How can i resolve this problem, and the recieved file will have the same content of the file sent, and without modification please:
    This is function to send the file from the client:
    @void FenClient::on_sendbuttom_clicked()
    {
    QString nomFichier =lineEdit->text();
    QFile file(nomFichier);
    if(!file.open(QIODevice::ReadOnly))
    {
    qDebug() << "Error file can't be opened ! !";
    return;

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

    }@

    and the function for recieving the file by the server:
    @void FenServeur::dataRecieved()
    {
    QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());

    QString filename="out.ddd";
    

    QFile file(filename);
    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();
        }
    

    }
    @


  • Moderators

    Please do not open a new thread, because the answers to your "previous thread":http://qt-project.org/forums/viewthread/20768/ were not sufficient. Continnue with the old thread. It is helpful to provide some more information and details what you have tried.

    Closing this thread.


Log in to reply
 

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