C++ 在Qt中通过TCP传输大型文件

C++ 在Qt中通过TCP传输大型文件,c++,qt,sockets,tcp,C++,Qt,Sockets,Tcp,我是通过套接字传输TCP文件的新手。因此,就自我修复而言,我想修改示例“环回”,使其在建立连接后从服务器向客户端发送一个大文件(例如100Mb到2Gb)。 我的问题是,我不知道如何分割文件,以便现在传输必须完成。 让我插入一段代码,使其更易于理解: server.cpp void Dialog::acceptConnection() { tcpServerConnection = tcpServer.nextPendingConnection(); connect(tcpServ

我是通过套接字传输TCP文件的新手。因此,就自我修复而言,我想修改示例“环回”,使其在建立连接后从服务器向客户端发送一个大文件(例如100Mb到2Gb)。 我的问题是,我不知道如何分割文件,以便现在传输必须完成。 让我插入一段代码,使其更易于理解:

server.cpp

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)));
}
void Dialog::acceptConnection()
{
tcpServerConnection=tcpServer.nextPendingConnection();
连接(tcpServerConnection,信号(connected()),此,插槽(startTransfer());
连接(tcpServerConnection,信号(bytesWrited(qint64)),此,插槽(updateServerProgress(qint64));
连接(tcpServerConnection,信号(错误(QAbstractSocket::SocketError)),此,插槽(显示错误(QAbstractSocket::SocketError));
serverStatusLabel->setText(tr(“已接受的连接”);
startTransfer();
}
无效对话框::startTransfer()
{
文件=新的QFile(“文件路径”);
如果(!文件->打开(QIODevice::ReadOnly))
{
serverStatusLabel->setText(“无法打开文件”);
返回;
}
int TotalBytes=文件->大小();
bytesToWrite=TotalBytes-(int)tcpServerConnection->write();
serverStatusLabel->setText(tr(“已连接”);
}
void Dialog::updateServerProgress(qint64个字节)
{
字节写入+=(int)个字节;
//只有在未完成且Qt写入缓冲区小于某个大小时,才能写入更多。
if(bytesToWrite>0&&tcpServerConnection->bytesToWrite()write());
serverProgressBar->setMaximum(TotalBytes);
serverProgressBar->setValue(字节写入);
serverStatusLabel->setText(tr(“已发送%1MB”).arg(字节写入/(1024*1024));
}
我见过一些使用
readAll()
的解决方案,但我认为qt无法处理内部有2Gb数据的缓冲区。。。 因此,如前所述,我的问题是如何通过重写
tcpServerConnection
将文件升级到?我想知道是否建议将QDataStream用于此目的(
QDataStream out(&file,QIODevice::WriteOnly);

谢谢


PD:注意代码上的标记。

好的,多亏了Basile Starynkevitch我找到了解决方案。 它与设置一样简单:

buffer = file->read(PayloadSize);
然后通过Tcp发送。在本地网络中,我可以在40.11秒内传输397Mb的数据。
谢谢,

只需将文件复制成中等大小的块(例如,每个块64 KB)