就不新建工程了,还是原来的味道,注意在工程文件中添加QT += network
client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QDialog>
#include <QAbstractSocket>
class QTcpSocket;
class QFile;
namespace Ui {
class Client;
}
class Client : public QDialog
{
Q_OBJECT
public:
explicit Client(QWidget *parent = 0);
~Client();
private:
Ui::Client *ui;
QTcpSocket *tcpClient;
QFile *localFile;
qint64 totalBytes;
qint64 bytesWritten;
qint64 bytesToWrite;
qint64 payloadSize;
QString fileName;
QByteArray outBlock;
private slots:
void openFile();
void send();
void startTransfer();
void updateClientProgress(qint64);
void displayError(QAbstractSocket::SocketError);
void on_openButton_clicked();
void on_sendButton_clicked();
};
#endif // CLIENT_H
client.cpp
#include "client.h"
#include "ui_client.h"
#include <QtNetwork>
#include <QFileDialog>
Client::Client(QWidget *parent) :
QDialog(parent),
ui(new Ui::Client)
{
ui->setupUi(this);
payloadSize = 64*1024; // 64KB
totalBytes = 0;
bytesWritten = 0;
bytesToWrite = 0;
tcpClient = new QTcpSocket(this);
// 当连接服务器成功时,发出connected()信号,开始传送文件
connect(tcpClient, SIGNAL(connected()), this, SLOT(startTransfer()));
connect(tcpClient, SIGNAL(bytesWritten(qint64)),
this, SLOT(updateClientProgress(qint64)));
connect(tcpClient, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
ui->sendButton->setEnabled(false);
}
Client::~Client()
{
delete ui;
}
void Client::openFile()
{
fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty()) {
ui->sendButton->setEnabled(true);
ui->clientStatusLabel->setText(tr("打开文件 %1 成功!").arg(fileName));
}
}
void Client::send()
{
ui->sendButton->setEnabled(false);
// 初始化已发送字节为0
bytesWritten = 0;
ui->clientStatusLabel->setText(tr("连接中…"));
tcpClient->connectToHost(ui->hostLineEdit->text(),
ui->portLineEdit->text().toInt());
}
void Client::startTransfer()
{
localFile = new QFile(fileName);
if (!localFile->open(QFile::ReadOnly)) {
qDebug() << "client: open file error!";
return;
}
// 获取文件大小
totalBytes = localFile->size();
QDataStream sendOut(&outBlock, QIODevice::WriteOnly);
sendOut.setVersion(QDataStream::Qt_4_0);
QString currentFileName = fileName.right(fileName.size()
- fileName.lastIndexOf('/')-1);
// 保留总大小信息空间、文件名大小信息空间,然后输入文件名
sendOut << qint64(0) << qint64(0) << currentFileName;
// 这里的总大小是总大小信息、文件名大小信息、文件名和实际文件大小的总和
totalBytes += outBlock.size();
sendOut.device()->seek(0);
// 返回outBolock的开始,用实际的大小信息代替两个qint64(0)空间
sendOut << totalBytes << qint64((outBlock.size() - sizeof(qint64)*2));
// 发送完文件头结构后剩余数据的大小
bytesToWrite = totalBytes - tcpClient->write(outBlock);
ui->clientStatusLabel->setText(tr("已连接"));
outBlock.resize(0);
}
void Client::updateClientProgress(qint64 numBytes)
{
// 已经发送数据的大小
bytesWritten += (int)numBytes;
// 如果已经发送了数据
if (bytesToWrite > 0) {
// 每次发送payloadSize大小的数据,这里设置为64KB,如果剩余的数据不足64KB,
// 就发送剩余数据的大小
outBlock = localFile->read(qMin(bytesToWrite, payloadSize));
// 发送完一次数据后还剩余数据的大小
bytesToWrite -= (int)tcpClient->write(outBlock);
// 清空发送缓冲区
outBlock.resize(0);
} else { // 如果没有发送任何数据,则关闭文件
localFile->close();
}
// 更新进度条
ui->clientProgressBar->setMaximum(totalBytes);
ui->clientProgressBar->setValue(bytesWritten);
// 如果发送完毕
if(bytesWritten == totalBytes) {
ui->clientStatusLabel->setText(tr("传送文件 %1 成功").arg(fileName));
localFile->close();
tcpClient->close();
}
}
void Client::displayError(QAbstractSocket::SocketError)
{
qDebug() << tcpClient->errorString();
tcpClient->close();
ui->clientProgressBar->reset();
ui->clientStatusLabel->setText(tr("客户端就绪"));
ui->sendButton->setEnabled(true);
}
// 打开按钮
void Client::on_openButton_clicked()
{
ui->clientProgressBar->reset();
ui->clientStatusLabel->setText(tr("状态:等待打开文件!"));
openFile();
}
// 发送按钮
void Client::on_sendButton_clicked()
{
send();
}
server.h
#ifndef SERVER_H
#define SERVER_H
#include <QDialog>
#include <QAbstractSocket>
#include <QTcpServer>
class QTcpSocket;
class QFile;
namespace Ui {
class Server;
}
class Server : public QDialog
{
Q_OBJECT
public:
explicit Server(QWidget *parent = 0);
~Server();
private:
Ui::Server *ui;
QTcpServer tcpServer;
QTcpSocket *tcpServerConnection;
qint64 totalBytes;
qint64 bytesReceived;
qint64 fileNameSize;
QString fileName;
QFile *localFile;
QByteArray inBlock;
private slots:
void start();
void acceptConnection();
void updateServerProgress();
void displayError(QAbstractSocket::SocketError socketError);
void on_startButton_clicked();
};
#endif // SERVER_H
server.cpp
#include "server.h"
#include "ui_server.h"
#include <QtNetwork>
Server::Server(QWidget *parent) :
QDialog(parent),
ui(new Ui::Server)
{
ui->setupUi(this);
connect(&tcpServer, SIGNAL(newConnection()),
this, SLOT(acceptConnection()));
}
Server::~Server()
{
delete ui;
}
void Server::start()
{
if (!tcpServer.listen(QHostAddress::LocalHost, 6666)) {
qDebug() << tcpServer.errorString();
close();
return;
}
ui->startButton->setEnabled(false);
totalBytes = 0;
bytesReceived = 0;
fileNameSize = 0;
ui->serverStatusLabel->setText(tr("监听"));
ui->serverProgressBar->reset();
}
void Server::acceptConnection()
{
tcpServerConnection = tcpServer.nextPendingConnection();
connect(tcpServerConnection, SIGNAL(readyRead()),
this, SLOT(updateServerProgress()));
connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
ui->serverStatusLabel->setText(tr("接受连接"));
tcpServer.close();
}
void Server::updateServerProgress()
{
QDataStream in(tcpServerConnection);
in.setVersion(QDataStream::Qt_4_0);
if (bytesReceived <= sizeof(qint64)*2) {
if((tcpServerConnection->bytesAvailable() >= sizeof(qint64)*2)
&& (fileNameSize == 0)) {
in >> totalBytes >> fileNameSize;
bytesReceived += sizeof(qint64) * 2;
}
if((tcpServerConnection->bytesAvailable() >= fileNameSize)
&& (fileNameSize != 0)) {
in >> fileName;
ui->serverStatusLabel->setText(tr("接收文件%1 ...")
.arg(fileName));
bytesReceived += fileNameSize;
localFile = new QFile(fileName);
if (!localFile->open(QFile::WriteOnly)) {
qDebug() << "server: open file error!";
return;
}
} else {
return;
}
}
if (bytesReceived < totalBytes) {
bytesReceived += tcpServerConnection->bytesAvailable();
inBlock = tcpServerConnection->readAll();
localFile->write(inBlock);
inBlock.resize(0);
}
ui->serverProgressBar->setMaximum(totalBytes);
ui->serverProgressBar->setValue(bytesReceived);
if (bytesReceived == totalBytes) {
tcpServerConnection->close();
localFile->close();
ui->startButton->setEnabled(true);
ui->serverStatusLabel->setText(tr("接受文件 %1 成功")
.arg(fileName));
}
}
void Server::displayError(QAbstractSocket::SocketError socketError)
{
qDebug() << tcpServerConnection->errorString();
tcpServerConnection->close();
ui->serverProgressBar->reset();
ui->serverStatusLabel->setText(tr("服务端就绪"));
ui->startButton->setEnabled(true);
}
void Server::on_startButton_clicked()
{
start();
}
注意在main.cpp中添加QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));防止中文乱码
界面效果如下:
文章出自:Linux_Google