@jsulm
I looked at my code and it seems that I got two example mixed up.
Here is the link. https://stackoverflow.com/questions/30288385/how-to-send-a-file-in-qt
So I changed a little bit in my code.
Her is the sender part.
void Communication::onSend() {
mTcpSocket->connectToHost(mIpAddress, mTcpPort);
if (mTcpSocket->waitForConnected(3000)) {
qInfo() << "Connection estabished.";
qDebug() << "Open File:" << mFileName;
QFile file(mDataPath + "/" + mFileName);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Some error occur. Could not open the file.";
return;
}
QFileInfo fileInfo(file);
qInfo() << "Filename:" << mFileName << "; size:" << fileInfo.size();
QDataStream out(mTcpSocket);
out << mFileName;
out << QString::number(fileInfo.size());
while (!file.atEnd()) {
QByteArray rawFile = file.read(5000);
out << rawFile;
mSize += rawFile.size();
qDebug() << "Send:" << mSize << "of:" << file.size();
qInfo() << rawFile;
}
} else {
qWarning() << "No connection established.";
return;
}
mTcpSocket->disconnectFromHost();
qDebug() << "Disconnected from the socket.";
}
And this is my receiver part.
void MyServer::readFromClient() {
QDataStream in(mTcpSocket);
QString fileName;
QString fileSize;
QByteArray data;
QFile file;
++mCounter;
qInfo() << "readFromClient counter:" << mCounter;
if (mCounter > 1000) {
qWarning() << "Counter to high. Close connection.";
mTcpSocket->close();
return;
}
if (mInfoRecieved) {
mInfoRecieved = false;
in >> fileName;
qDebug() << "Recieved file name:" << fileName;
in >> fileSize;
qDebug() << "recieved file size:" << fileSize;
if (!fileName.isEmpty()) {
mFileName = fileName;
}
}
file.setFileName(mFileName);
if (file.open(QIODevice::Append)) {
while (mTcpSocket->bytesAvailable()) {
qDebug() << "Bytes available:" << mTcpSocket->bytesAvailable();
in >> data;
qDebug() << data;
file.write(data);
}
file.close();
}
}
I let me output some information for the sender and the receiver.
The sender give me this.
I SensorLogger: ../SensorLogger/src/communication.cpp:70 (void Communication::onSend()): Connection estabished.
D SensorLogger: ../SensorLogger/src/communication.cpp:72 (void Communication::onSend()): Open File: "170906093258.xml"
I SensorLogger: ../SensorLogger/src/communication.cpp:82 (void Communication::onSend()): Filename: "170906093258.xml" ; size: 127977
D SensorLogger: ../SensorLogger/src/communication.cpp:96 (void Communication::onSend()): Send: 5000 of: 127977
// Here is the output of the data so I can check if I have an error while reading the information from the file
D SensorLogger: ../SensorLogger/src/communication.cpp:96 (void Communication::onSend()): Send: 10000 of: 127977
D SensorLogger: ../SensorLogger/src/communication.cpp:96 (void Communication::onSend()): Send: 15000 of: 127977
D SensorLogger: ../SensorLogger/src/communication.cpp:96 (void Communication::onSend()): Send: 20000 of: 127977
D SensorLogger: ../SensorLogger/src/communication.cpp:96 (void Communication::onSend()): Send: 25000 of: 127977
D SensorLogger: ../SensorLogger/src/communication.cpp:96 (void Communication::onSend()): Send: 30000 of: 127977
D SensorLogger: ../SensorLogger/src/communication.cpp:96 (void Communication::onSend()): Send: 35000 of: 127977
D SensorLogger: ../SensorLogger/src/communication.cpp:96 (void Communication::onSend()): Send: 40000 of: 127977
D SensorLogger: ../SensorLogger/src/communication.cpp:96 (void Communication::onSend()): Send: 45000 of: 127977
D SensorLogger: ../SensorLogger/src/communication.cpp:96 (void Communication::onSend()): Send: 50000 of: 127977
D SensorLogger: ../SensorLogger/src/communication.cpp:96 (void Communication::onSend()): Send: 55000 of: 127977
D SensorLogger: ../SensorLogger/src/communication.cpp:96 (void Communication::onSend()): Send: 65000 of: 127977
This is what I get from the receiver part.
we have a new connection!
readFromClient counter: 1
Recieved file name: "170906093258.xml"
recieved file size: "127977"
Bytes available: 1396
""
readFromClient counter: 2
Bytes available: 1448
""
readFromClient counter: 3
Bytes available: 1448
""
readFromClient counter: 4
Bytes available: 1448
""
readFromClient counter: 5
Bytes available: 1448
""
readFromClient counter: 6
Bytes available: 1448
""
readFromClient counter: 7
Bytes available: 1448
""
readFromClient counter: 8
Bytes available: 1448
""
.
.
.
readFromClient counter: 64
Bytes available: 1448
""
readFromClient counter: 65
Bytes available: 1448
""
readFromClient counter: 66
Bytes available: 1448
""
readFromClient counter: 67
Bytes available: 709
""
I don't understand why the amount of byte read are so different from the amount I write to the socket.