qt5服务器接收发送文件
时间: 2023-10-09 14:17:22 浏览: 196
1103_01_server_3.zip
要实现Qt5服务器接收和发送文件,可以使用Qt的网络模块以及文件输入输出模块。以下是一个简单的示例:
1. 接收文件
服务器端:
```cpp
QTcpSocket* clientConnection; // 已连接的客户端套接字
void MyServer::readyRead() {
QByteArray block = clientConnection->readAll(); // 读取客户端发送的数据
QDataStream in(&block, QIODevice::ReadOnly);
in.setVersion(QDataStream::Qt_5_9);
if (block.size() == sizeof(quint64)) { // 如果是文件大小信息
in >> totalBytes;
bytesReceived = 0;
qDebug() << "Total bytes: " << totalBytes;
// 打开一个文件,准备写入数据
localFile = new QFile(fileName);
if (!localFile->open(QFile::WriteOnly)) {
qDebug() << "Server: open file error!";
return;
}
} else if (bytesReceived < totalBytes) { // 如果是文件数据
bytesReceived += clientConnection->bytesAvailable();
inBlock = clientConnection->readAll();
localFile->write(inBlock);
inBlock.resize(0);
qDebug() << "Bytes received: " << bytesReceived;
}
}
```
客户端:
```cpp
QTcpSocket* tcpSocket; // 已连接的服务器套接字
QFile* localFile; // 要发送的文件指针
QByteArray outBlock; // 存储文件数据的字节数组
qint64 fileSize; // 文件大小
void MyClient::sendFile() {
tcpSocket->connectToHost(ipAddr, portNum);
if (!tcpSocket->waitForConnected()) {
qDebug() << "Client: connect error!";
return;
}
localFile = new QFile(filePath);
if (!localFile->open(QFile::ReadOnly)) {
qDebug() << "Client: open file error!";
return;
}
fileSize = localFile->size();
outBlock.resize(sizeof(qint64));
QDataStream out(&outBlock, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_9);
out << fileSize; // 先发送文件大小信息
tcpSocket->write(outBlock); // 发送文件大小信息
outBlock.resize(0);
while (!localFile->atEnd()) { // 发送文件数据
outBlock = localFile->read(qMin(bytesToWrite, fileSize - bytesWritten));
bytesWritten += tcpSocket->write(outBlock);
outBlock.resize(0);
qDebug() << "Bytes written: " << bytesWritten;
}
localFile->close();
tcpSocket->close();
}
```
2. 发送文件
服务器端:
```cpp
QTcpSocket* clientConnection; // 已连接的客户端套接字
QFile* localFile; // 要发送的文件指针
QByteArray outBlock; // 存储文件数据的字节数组
qint64 fileSize; // 文件大小
void MyServer::sendFile() {
clientConnection->connectToHost(ipAddr, portNum);
if (!clientConnection->waitForConnected()) {
qDebug() << "Server: connect error!";
return;
}
localFile = new QFile(filePath);
if (!localFile->open(QFile::ReadOnly)) {
qDebug() << "Server: open file error!";
return;
}
fileSize = localFile->size();
outBlock.resize(sizeof(qint64));
QDataStream out(&outBlock, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_9);
out << fileSize; // 先发送文件大小信息
clientConnection->write(outBlock); // 发送文件大小信息
outBlock.resize(0);
while (!localFile->atEnd()) { // 发送文件数据
outBlock = localFile->read(qMin(bytesToWrite, fileSize - bytesWritten));
bytesWritten += clientConnection->write(outBlock);
outBlock.resize(0);
qDebug() << "Bytes written: " << bytesWritten;
}
localFile->close();
clientConnection->close();
}
```
客户端:
```cpp
QTcpSocket* tcpSocket; // 已连接的服务器套接字
void MyClient::readyRead() {
QByteArray block = tcpSocket->readAll(); // 读取服务器发送的数据
QDataStream in(&block, QIODevice::ReadOnly);
in.setVersion(QDataStream::Qt_5_9);
if (block.size() == sizeof(quint64)) { // 如果是文件大小信息
in >> totalBytes;
bytesReceived = 0;
qDebug() << "Total bytes: " << totalBytes;
// 打开一个文件,准备写入数据
localFile = new QFile(fileName);
if (!localFile->open(QFile::WriteOnly)) {
qDebug() << "Client: open file error!";
return;
}
} else if (bytesReceived < totalBytes) { // 如果是文件数据
bytesReceived += tcpSocket->bytesAvailable();
inBlock = tcpSocket->readAll();
localFile->write(inBlock);
inBlock.resize(0);
qDebug() << "Bytes received: " << bytesReceived;
}
}
```
以上代码仅供参考,需要根据实际需求进行修改。
阅读全文