Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
File transfer with QtcpSocket
cerina last edited by
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:
QString nomFichier =lineEdit->text();
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.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:
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
qDebug("File cannot be opened.");
QByteArray read = socket->read(socket->bytesAvailable()); qDebug() << "Read : " << read.size(); file.write(read); file.close(); }
koahnig last edited by
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.