sendOut << qint64(0) << qint64(0) << currentFileName;的含义
时间: 2024-06-03 07:11:44 浏览: 6
这段代码是将文件名和文件大小一起写入一个数据流中。`sendOut`是一个QDataStream对象,`qint64(0)`作为占位符,表示文件大小,因为在写入实际的文件大小之前,我们不知道它的值,因此我们先写入0。接下来的`currentFileName`是文件的名称。这样,我们就可以将文件名和大小一起发送到接收端。在接收端,我们可以先读取文件名,然后读取文件大小,并根据文件大小来接收文件的内容。
相关问题
ui->serverSendBtn->setEnabled(false); clientConnection = tcpServer->nextPendingConnection(); connect(clientConnection, SIGNAL(bytesWritten(qint64)), this, SLOT(updateClientProgress(qint64))); ui->serverStatusLabel->setText(tr("开始传送文件 %1 !").arg(theFileName)); localFile = new QFile(fileName); if(!localFile->open((QFile::ReadOnly))){ QMessageBox::warning(this, tr("应用程序"), tr("无法读取文件 %1:\n%2") .arg(fileName).arg(localFile->errorString())); return; } TotalBytes = localFile->size(); QDataStream sendOut(&outBlock, QIODevice::WriteOnly); sendOut.setVersion(QDataStream::Qt_4_7); time.start(); // 开始计时 QString currentFile = fileName.right(fileName.size() - fileName.lastIndexOf('/')-1); sendOut << qint64(0) << qint64(0) << currentFile; TotalBytes += outBlock.size(); sendOut.device()->seek(0); sendOut << TotalBytes << qint64((outBlock.size() - sizeof(qint64)*2)); bytesToWrite = TotalBytes - clientConnection->write(outBlock); outBlock.resize(0);这段代码用qt编写,他的作用是什么
这段代码是一个QT应用程序中传送文件的部分,当服务器端收到客户端的连接请求时,就会执行此函数。该函数会将要发送的文件信息打包成一个数据流QDataStream,然后发送给客户端。其中,QDataStream将数据流序列化,并且设置了版本号为Qt_4_7,sendOut.device()->seek(0)将数据流的指针指向数据流的开头,然后将总字节数和数据块大小写入到数据流中,最后通过clientConnection->write(outBlock)将数据块发送给客户端。在发送数据块之后,服务器端会开始计时,用于计算文件传输的速度。
void Server::sndMsg() { ui->sSendBtn->setEnabled(false); clntConn = tSrv->nextPendingConnection(); connect(clntConn, SIGNAL(bytesWritten(qint64)), this, SLOT (updClntProgress(qint64))); ui->sStatusLbl->setText(tr("开始传输文件 %1 !").arg(theFileName)); locFile = new QFile(fileName); //只读方式打开 if(!locFile->open((QFile::ReadOnly))){ QMessageBox::warning(this, tr(" 应 用 程 序 "), tr(" 无 法 读 取 文 件 %1:\n%2").arg(fileName).arg(locFile->errorString())); return; } //获取待发送文件的大小 totalBytes = locFile->size(); //将发送缓冲区outBlock封装在一个QDataStream类型的变量中,这样做可以很方便通过重载"<<"操作符填写文件头结构 QDataStream sendOut(&outBlock, QIODevice::WriteOnly); sendOut.setVersion(QDataStream::Qt_4_7); time.start(); //开始计时--用来统计传输所用的时间 QString curFile = fileName.right(fileName.size() - fileName.lastIndexOf('/')-1);//去掉文件的路径部分 sendOut << qint64(0) << qint64(0) << curFile;//构造一个临时的头文件,将该值追加到totalBytes字段,从而完成实际需发送字节数的记录 totalBytes += outBlock.size(); sendOut.device()->seek(0);//将读写操作指向从头开始 sendOut << totalBytes << qint64((outBlock.size() - sizeof(qint64)*2));//填写实际的总长度和文件长度 bytesTobeWrite = totalBytes - clntConn->write(outBlock);//将改文件头发出,同时修改待发送字节数bytesTobeWrite outBlock.resize(0); //清空发送缓冲区以备下次使用 }
这段代码是一个 Qt 程序中的一个服务器类的槽函数,用于处理发送文件的请求。
在函数中,首先禁用了发送按钮,然后通过 `nextPendingConnection()` 函数获取一个新的客户端连接,即要接收文件的客户端。接着,连接了客户端连接的 `bytesWritten()` 信号,并将其与 `updClntProgress()` 槽函数连接起来,用于更新客户端的传输进度。
接下来,打开待发送的文件,并获取其大小。然后,将发送缓冲区 `outBlock` 封装在一个 `QDataStream` 类型的变量 `sendOut` 中,并设置其版本为 `Qt_4_7`。接着,通过 `sendOut` 向缓冲区写入一个临时的头文件,用于记录实际需发送字节数。然后,将 `sendOut` 的读写操作指向从头开始,并将实际的总长度和文件长度填写到头文件中。最后,将头文件发出,并修改待发送字节数 `bytesTobeWrite`。发送缓冲区 `outBlock` 则被清空以备下次使用。