Quenix
3rd May 2013, 01:45
Hello all,
I'm new to QT, new to this forum too, student programmer, etc. In brief, new.
I tried the following example in order to learned how to do a tcp connection and how to send some bytes too.
example : http://www.qtcentre.org/threads/48209-file-transfer-through-sockets , code in the bottom of the page.
Everything work well except data reading from a file.
I wrote (copy) that code in the server part :
// Send File
QFile inputFile(FILENAME);
QByteArray read;
inputFile.open(QIODevice::ReadOnly);
while(1)
{
read.clear();
read = inputFile.read(32768*8);
qDebug() << "Read : " << read.size();
qDebug() << "Pos : " << inputFile.pos();
if(read.size() == 0)
break;
qDebug() << "Written :" << client.write(read);
client.waitForBytesWritten();
}
So, it works well, but it stop after 3 pass (while) and just sent a certain amount of bytes even if I use whatever file as input.
It seems that is the read = inputFile.read(32768*8) that is wrong, but I'm not sure.
Following is the server output. In the third pass it just read 182276 bytes and the QTCpSocket client.write(read) return -1 this time.
The amount of data received by the client = the amount sent by the server = 706564.
File transfer started
Thread called
Thread Descriptor : 21
Thread : Connected
Read : 262144
Pos : 262144
Written : 262144
Read : 262144
Pos : 524288
Written : 262144
Read : 182276
Pos : 706564
Written : -1
QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState
Read : 0
Pos : 706564
QAbstractSocket::waitForDisconnected() is not allowed in UnconnectedState
Thread : File transfer completed
/home/serge/Bureau/tp6-server/tp6-server s'est terminé avec le code 0
Thank you very much for your time.
I'm new to QT, new to this forum too, student programmer, etc. In brief, new.
I tried the following example in order to learned how to do a tcp connection and how to send some bytes too.
example : http://www.qtcentre.org/threads/48209-file-transfer-through-sockets , code in the bottom of the page.
Everything work well except data reading from a file.
I wrote (copy) that code in the server part :
// Send File
QFile inputFile(FILENAME);
QByteArray read;
inputFile.open(QIODevice::ReadOnly);
while(1)
{
read.clear();
read = inputFile.read(32768*8);
qDebug() << "Read : " << read.size();
qDebug() << "Pos : " << inputFile.pos();
if(read.size() == 0)
break;
qDebug() << "Written :" << client.write(read);
client.waitForBytesWritten();
}
So, it works well, but it stop after 3 pass (while) and just sent a certain amount of bytes even if I use whatever file as input.
It seems that is the read = inputFile.read(32768*8) that is wrong, but I'm not sure.
Following is the server output. In the third pass it just read 182276 bytes and the QTCpSocket client.write(read) return -1 this time.
The amount of data received by the client = the amount sent by the server = 706564.
File transfer started
Thread called
Thread Descriptor : 21
Thread : Connected
Read : 262144
Pos : 262144
Written : 262144
Read : 262144
Pos : 524288
Written : 262144
Read : 182276
Pos : 706564
Written : -1
QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState
Read : 0
Pos : 706564
QAbstractSocket::waitForDisconnected() is not allowed in UnconnectedState
Thread : File transfer completed
/home/serge/Bureau/tp6-server/tp6-server s'est terminé avec le code 0
Thank you very much for your time.