Envoyer un fichier par TCP
Bonjour,
J'ai un souci pour envoyer un fichier en tcp ip sur qtcreator :
Pourriez vous m'aider svp?
la connection tcp ip se fait
le fichier est detecter mais aucune copie du contenu
Merci :ccool:
send :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| void simpleCommunication::sendFile()
{
QFile file("D:/tcp ip/QTandTCP-serverQT-ClientQT/QtSendFile/aging.stm");
if (!file.open(QIODevice::ReadOnly))
QMessageBox::information(this, tr("File"),tr("File cannot be opened."));
QByteArray ba;
QDataStream out(&ba, QIODevice::WriteOnly);
out << (quint16)0;
while (!file.atEnd())
{
QByteArray line = file.readLine();
QString om(line);
out<<om;
out.device()->seek(0);
out << (quint16)(ba.size() - sizeof(quint16));
}
file.close();
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
clientConnection->write(ba);
clientConnection->disconnectFromHost();
} |
Reception :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| void simpleCommunication::readFile()
{
QDataStream in (tcpSocket);
qint64 size;
QFile file("D:/tcp ip/QTandTCP-serverQT-ClientQT/QtReceiveFile/aging.stm");
if (!file.open(QIODevice::Append))
QMessageBox::information(this, tr("File"),tr("File cannot be opened."));
QTextStream txtStrmForFile(&file);
forever
{
if (size == 0)
{
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> size;
}
if (tcpSocket->bytesAvailable() < size)
return;
QByteArray ba;
in>>ba;
txtStrmForFile<<ba;
ba="";
size=0;
}
file.close();
statusLabel->setText(currentFortune);
getFortuneButton->setEnabled(true);
} |