在Qt中通过TCP传输大文件
Transferring large files over TCP in Qt
我是通过套接字传输TCP文件的新手。因此,就自我转换而言,我想修改示例"环回",使其在建立连接后从服务器到客户端发送大文件(例如 100Mb 到 2Gb)。我的问题是我不知道如何拆分文件以便现在传输必须完成。让我插入一段代码,使其更容易理解:
服务器.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)));
}
我已经看到了一些使用readAll()
的解决方案,但我认为 qt 无法处理内部包含 2Gb 数据的缓冲区......因此,如前所述,我的问题是如何通过tcpServerConnection
重写来滑倒文件?我想知道是否建议为此目的使用QDataStream(QDataStream out(&file, QIODevice::WriteOnly);
),以及。
谢谢!
PD:注意标记<-这里是什么!!->代码上。
好的,多亏了Basile Starynkevitch,我找到了解决方案。就像设置一样简单:
buffer = file->read(PayloadSize);
然后通过 Tcp 发送。在本地网络中,我实现了在40.11秒内传输397Mb。谢谢
相关文章:
- 如何在标准c ++中流式传输/读取二进制文件的中间部分并写入另一个文件?
- 转换 mp4 文件并将其流式传输到带有树莓派的查看器
- QTCPSocket(Qt-C++)中的音频文件逐字节传输
- 传输文件一次又一次地发送相同的字节
- C++文件传输的校验和算法
- 如何在Ubuntu中将内容从一个文件传输到另一个文件
- 如何在 mfc c++ 中流式传输.zip文件夹的二进制表示形式
- 在计算机之间使用C 传输文件
- 使用 MQTT 进行文件传输
- 如何将数据类型声明从.cpp文件传输到 .cu 文件
- 通过TCP在客户端/ C#服务器之间传输文件C++
- 如何将字符串形式的值从类文件传输到输出文件
- 通过USB将文件从iOS设备传输到Windows PC
- TCP/IP文件传输C
- 为什么文件没有通过 tcp 在 C# 中的客户端和 C 中的服务器之间正确传输(保存?)
- 如何确保TCP文件传输完成(c++)
- 在web浏览器上流式传输c++文件
- 使用以太网电缆在两台 Mac 之间自动传输文件
- C++Libssh-服务器/客户端通过局域网传输文件的例子
- c++以相反的顺序逐行传输文件