나는 소켓을 통한 TCP 파일 전송에 초보자입니다. 자립 관점에서 볼 때 연결이 설정되면 서버에서 클라이언트로 큰 파일 (예 : 100MB ~ 2Gb)을 보내도록 예제 "루프백"을 수정하고 싶습니다. 내 문제는 전송을 마칠 때까지 파일을 분할하는 방법을 알지 못한다는 것입니다. 내가하지만 QT가 처리 할 수 있다고 생각하지 않습니다 readAll()
을 사용하는 일부 솔루션을 본 적이Qt에서 TCP를 통해 대용량 파일 전송하기
void Dialog::acceptConnection()
{
tcpServerConnection = tcpServer.nextPendingConnection();
connect(tcpServerConnection,SIGNAL(connected()), this, SLOT(startTransfer()));
connect(tcpServerConnection, SIGNAL(bytesWritten(qint64)), this, SLOT(updateServerProgress(qint64)));
connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
serverStatusLabel->setText(tr("Accepted connection"));
startTransfer();
}
void Dialog::startTransfer()
{
file = new QFile("file_path");
if (!file->open(QIODevice::ReadOnly))
{
serverStatusLabel->setText("Couldn't open the file");
return;
}
int TotalBytes = file->size();
bytesToWrite = TotalBytes - (int)tcpServerConnection->write(<-WHAT HERE!!!->));
serverStatusLabel->setText(tr("Connected"));
}
void Dialog::updateServerProgress(qint64 numBytes)
{
bytesWritten += (int)numBytes;
// only write more if not finished and when the Qt write buffer is below a certain size.
if (bytesToWrite > 0 && tcpServerConnection->bytesToWrite() <= 4*PayloadSize)
bytesToWrite -= (int)tcpServerConnection->write(<-WHAT HERE!!!->));
serverProgressBar->setMaximum(TotalBytes);
serverProgressBar->setValue(bytesWritten);
serverStatusLabel->setText(tr("Sent %1MB").arg(bytesWritten/(1024 * 1024)));
}
server.cpp : 날이 쉽게 이해할 수 있도록 코드 조각을 삽입하자 내부 데이터의 2Gb와 버퍼 ... 그래서, 언급했듯이, 내 질문은 tcpServerConnection
를 통해 작성하여 파일을 슬립하는 방법입니까? 그 목적으로 QDataStream을 사용하는 것이 좋은지 알고 싶습니다 (QDataStream out(&file, QIODevice::WriteOnly);
).
감사합니다.
PD : 코드에 < -WHAT 여기! ->을 유의하십시오.
중간 크기의 청크 (예 : 각각 64Kbytes)로 파일을 복사하기 만하면됩니다. –