sendFile = new QFile(ui->fileLabel->text()); QFileInfo fileInfo(sendFile->fileName()); QString fileName(fileInfo.fileName()); socket->waitForConnected(); QByteArray block; QDataStream read(sendFile); sendFile->open(QFile::ReadOnly); //отправка служебной информации QByteArray bArray; QDataStream dStream(&bArray, QIODevice::WriteOnly); dStream.setVersion(QDataStream::Qt_4_5); dStream << quint64(sendFile->size()); // dStream << CRC32(filePath); // //qDebug () << "CRC sended: " << CRC32(filePath); // dStream << filePath; // dStream << inPath; socket->write(bArray); socket->waitForBytesWritten(); long int lBytes = 0; char ch [1024]; ch[1023] = '\0'; int i=0; while(!read.atEnd()){ int l = read.readRawData(ch, sizeof(char)*1023); QByteArray ba(ch, sizeof(char)*l); lBytes += socket->write(ba, sizeof(char)*l); socket->waitForBytesWritten(); if (-1 == lBytes){ qWarning() << "Error"; socket->close(); return; } } free((void*)ch);
QTcpSocket* socket = (QTcpSocket*)sender(); quint64 fileSize; QString fileName; QByteArray receivedLine; socket->waitForReadyRead(-1); QByteArray bArray; QDataStream dStream(&bArray, QIODevice::ReadWrite); dStream.setVersion(QDataStream::Qt_4_5); bArray = socket->readAll(); dStream >> fileSize; dStream >> fileName; dStream >> receivedLine; QFile receivedFile("E:/test/1.txt"); receivedFile.open(QFile::WriteOnly); QDataStream write(&receivedFile); write.setVersion(QDataStream::Qt_4_5); long int lBytesDone = 0; long int lSize = fileSize; long int lBytes; while (lBytesDone < lSize) { lBytes = 0; while (lBytes == 0) lBytes = socket->waitForReadyRead(-1); QByteArray tmp = socket->readAll(); lBytes += write.writeRawData(tmp.data(), tmp.size()); lBytesDone += tmp.size(); } receivedFile.close();}
while (lBytes == 0) lBytes = socket->waitForReadyRead(-1);
dStream << quint64(sendFile->size()); dStream << fileName;
dStream >> fileSize; dStream >> fileName;
bArray = socket->readAll();
QFile *sendFile = new QFile(...имя файла...); QFileInfo fileInfo(sendFile->fileName()); QDataStream read(sendFile); sendFile->open(QFile::ReadOnly); read.setVersion(QDataStream::Qt_5_1); //отправка служебной информации QByteArray bArray; QDataStream dStream(&bArray, QIODevice::WriteOnly); dStream.setVersion(QDataStream::Qt_5_1); dStream << quint64(sendFile->size()); dStream << fileInfo.fileName(); pSocket->write(bArray); pSocket->waitForBytesWritten(); long int lBytes = 0; char ch[1024]; ch[1023] = '\0'; int i=0; while(!read.atEnd()) { int l = read.readRawData(ch, sizeof(char)*1023); QByteArray ba(ch, sizeof(char)*l); lBytes = pSocket->write(ba, sizeof(char)*l); pSocket->waitForBytesWritten(); qDebug()<<"ba.size()"<<ba.size(); if (-1 == lBytes) { qWarning() << "Error"; pSocket->close(); return; } } free((void*)ch); pSocket->close();
quint64 fileSize; QString fileName; QByteArray bArray; QDataStream dStream(&bArray, QIODevice::ReadWrite); dStream.setVersion(QDataStream::Qt_5_1); pSocket.waitForReadyRead(-1); bArray = pSocket.readAll(); dStream >> fileSize; dStream >> fileName; emit doUpdateUI(QString::number(fileSize)); emit doUpdateUI(fileName); QFile saveFile("C:/"+fileName); saveFile.open(QFile::WriteOnly); QDataStream write(&saveFile); write.setVersion(QDataStream::Qt_5_1); long int lBytesDone = 0; long int lSize = fileSize; long int lBytes; while (lBytesDone < lSize) { lBytes = 0; while (lBytes == 0) { lBytes = pSocket.waitForReadyRead(-1); } if (-1 == lBytes) {//Если произошла ошибка сокета, то qDebug () << "EROOR!!"; pSocket.close(); return; }//if (-1 == lBytes) QByteArray tmp = pSocket.readAll(); qDebug()<<"tmp.size();"<<tmp.size(); lBytes += write.writeRawData(tmp.data(), tmp.size()); lBytesDone += tmp.size(); } saveFile.close();
C++ (Qt)free((void*)ch);
void MyClient::slotSendToServer(){ QFile *sendFile = new QFile("C:\\Users\\1\\Desktop\\2.exe"); //QFileInfo fileInfo(sendFile->fileName()); //QString fileName(fileInfo.fileName()); socket->waitForConnected(); QDataStream read(sendFile); sendFile->open(QFile::ReadOnly); //отправка служебной информации QByteArray bArray; QDataStream dStream(&bArray, QIODevice::WriteOnly); dStream.setVersion(QDataStream::Qt_4_8); dStream << quint64(sendFile->size()-sizeof(quint64)); socket->write(bArray); socket->waitForBytesWritten(); long int lBytes = 0; char ch [1024]; ch[1023] = '\0'; int i=0; while(!read.atEnd()){ int l = read.readRawData(ch, sizeof(char)*1023); QByteArray ba(ch, sizeof(char)*l); lBytes += socket->write(ba, sizeof(char)*l); qDebug()<<"l:" << lBytes; socket->waitForBytesWritten(); if (-1 == lBytes){ qWarning() << "Error"; socket->close(); return; } } qDebug()<<"sendFile->size(): "<<sendFile->size(); //free((void*)ch);}
void MyServer::slotReadClient() // считывает сообщение от клиента{ QTcpSocket* socket = (QTcpSocket*)sender(); quint64 fileSize; QString fileName; QByteArray receivedLine; socket->waitForReadyRead(-1); QByteArray bArray; QDataStream dStream(&bArray, QIODevice::ReadWrite); dStream.setVersion(QDataStream::Qt_4_8); bArray = socket->readAll(); dStream >> fileSize; qDebug()<<"recive file size: "<<fileSize; QFile receivedFile("C:\\Users\\1\\Desktop\\folder\\2.exe"); receivedFile.open(QFile::WriteOnly); QDataStream write(&receivedFile); write.setVersion(QDataStream::Qt_4_8); long int lBytesDone = 0; long int lSize = fileSize; long int lBytes; qDebug()<<"lSize: "<<lSize; while (lBytesDone < lSize) { lBytes = 0; while (lBytes == 0) lBytes = socket->waitForReadyRead(-1); QByteArray tmp = socket->readAll(); lBytes += write.writeRawData(tmp.data(), tmp.size()); lBytesDone += tmp.size(); qDebug()<<"l:" << lBytesDone; } receivedFile.close();}
l: 789756 l: 789864 sendFile->size(): 789864
l: 787710 l: 788733 l: 788841 Программа неожиданно завершилась.