PDA

View Full Version : Unable to send database file over LAN



webjack
7th October 2013, 12:55
Hi All,

I am developing a LAN application using Qt on Windows, I need to send a database file from clients to the server but I am unable to send the file properly the server is only receiving incomplete file or only signature of file which I am able to see in the destination folder but the file is empty.


The server code is:


void Mainapp::newConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
QFile file("C:/Qt/DB2");
file.remove();
file.open(QIODevice::Append);
socket->waitForReadyRead(1000);
qDebug()<<"hello kitty";
while(socket->bytesAvailable())
{
qDebug()<<"yes";
QByteArray bytes = socket->readLine();
file.write(bytes);
}




socket->close();
}

void Mainapp::broadcastDatagram()
{

QString str;
foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
{

udpSocket->writeDatagram("127.0.0.1", QHostAddress::Broadcast, 45454);
}
}
}


The client code is:


void Datafeed::connected()
{
qDebug()<<"Connected...";
QFile myFile("C:/Qt/DB");
myFile.open(QIODevice::ReadOnly);
QByteArray data= myFile.readAll();
QByteArray block;

QDataStream out(&block, QIODevice::WriteOnly);


out << (quint32)0;

out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));

msocket->write(data);
qDebug()<<data.data();


}


Thanks in advance for any help.

ChrisW67
7th October 2013, 21:15
You are assuming that the entire file arrives and is announced by a single readyRead() signal. This will not be the case and is the cause of your truncated file.
You are also treating your incoming data stream as if it was text, (e.g. With readline()) which is at odds with the QDataStream you used at the other end.