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);
    qDebug() << "Error file can't be opened ! !";

        QByteArray bytes = file.readAll();
        QByteArray block;
        QDataStream out(&bytes, QIODevice::WriteOnly);
        out << (quint32)0;
        out << nomFichier;
        out << bytes;
        out << quint32((bytes.size() -sizeof(quint32)));
        qDebug() << "Etat : envoi en cours...";
         listeMessages->append("Etat :sending...");
            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);
    qDebug("File cannot be opened.");
    qDebug("File created.");

        QByteArray read = socket->read(socket->bytesAvailable());
        qDebug() << "Read    : " << read.size();


  • Moderators

    Please do not open a new thread, because the answers to your "previous thread": 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.