Sorry for my bad english, but iḿ trying to learn the concept of data transfer between qtserver qtcpsocket, but my files on to receive are corrupt. what is wrong? I Need your help please, thanks.:(
Printable View
Sorry for my bad english, but iḿ trying to learn the concept of data transfer between qtserver qtcpsocket, but my files on to receive are corrupt. what is wrong? I Need your help please, thanks.:(
This is the receiver
Code:
void Dialog::tcpReady() { while(socket.bytesAvailable() > 0) { ds << ar; } file.close(); }
This is the Sender
Code:
void ServerThread::run() { QTcpSocket socket; if( !socket.setSocketDescriptor( m_descriptor ) ) { qDebug( "Socket error!" ); return; } { qDebug("Cannot open file!"); return; } int size=data.size(); QByteArray sizeBytes; quint32 b= quint32(data.size() - sizeof(quint32)); sizeBytes.append(b); sizeBytes.append(data); socket.write(sizeBytes); socket.waitForDisconnected(); }
Hi, gabizzz. The first slot doesn't have to be a loop, as socket.readAll() always reads everything in the buffer.
So instead try this, it should do the same:
Code:
void Dialog::tcpReady() { ds << socket.readAll(); file.close(); }
Better check you error conditions though. If the file open fails, your program will misbehave. I slipped in QIODevice::Append, assuming you don't want to overwrite your file for every received packet. For improved performance it's best to leave the file open and not reopen it for each packet though.
The rest of the code looks quite right (I haven't seen the whole source code).