#include "widget.h"
#include "ui_widget.h"
#include <QtNetwork>
#include <QFileDialog>
#pragma execution_character_set("utf-8")
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("文件传输客户端");
ui->sendButton->setEnabled(false);
m_payloadSize=64*1024;
m_totalBytes=0;
m_bytesWritten=0;
m_bytesToWrite=0;
ui->hostLineEdit->setText("127.0.0.1");
ui->portLineEdit->setText("9999");
m_tcpClient = new QTcpSocket(this);
// m_tcpClient->connectToHost()连接成功后会调用此函数
connect(m_tcpClient,SIGNAL(connected()),this,SLOT(startTransfer()));
// 执行m_tcpClient->write(m_outBlock)会触发此函数进行文件传输
connect(m_tcpClient,SIGNAL(bytesWritten(qint64)),this,SLOT(updateClientProgress(qint64)));
// m_tcpClient连接和传输过程中出错,均会调用此函数进行提示
connect(m_tcpClient,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
// connect(ui->sendButton,SIGNAL(clicked(bool)),this,SLOT(sendBtnClicked()));
// connect(ui->openButton,SIGNAL(clicked(bool)),this,SLOT(openBtnClicked()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::openFile()
{
m_fileName=QFileDialog::getOpenFileName(this);
if(!m_fileName.isEmpty())
{
ui->sendButton->setEnabled(true);
ui->clientStatusLabel->setText(QString("文件打开成功,请点击发送!"));
ui->lineEdit->setText(m_fileName);
}
}
void Widget::sendFile()
{
ui->sendButton->setEnabled(false);
m_bytesWritten=0;
ui->clientStatusLabel->setText("正在连接中...");
m_tcpClient->connectToHost(ui->hostLineEdit->text(),ui->portLineEdit->text().toInt());
}
void Widget::startTransfer()
{
// 打开需要传输的文件
m_localFile = new QFile(m_fileName);
if(!m_localFile->open(QFile::ReadOnly))
{
ui->clientStatusLabel->setText("文件打开失败!");
return;
}
// 初始化总大小字节数
m_totalBytes = m_localFile->size();
// 定义数据流
QDataStream sendOut(&m_outBlock,QIODevice::WriteOnly);
sendOut.setVersion(QDataStream::Qt_5_9);
QString strFileName = m_fileName.right(m_fileName.size()- m_fileName.lastIndexOf('/')-1);
//数据流中写入文件总大小、文件名大小、文件名,qint64(0)占位用
sendOut << qint64(0) << qint64(0) << strFileName;
// 修改总大小字节数
m_totalBytes += m_outBlock.size();
//修改数据流中写入的文件总大小、文件名大小
sendOut.device()->seek(0);
sendOut << m_totalBytes << qint64(m_outBlock.size()-sizeof(qint64)*2);
// 先调用m_tcpClient->write(m_outBlock)把m_outBlock中的文件总大小qint64、文件名大小qint64、文件名QString发出去
m_bytesToWrite = m_totalBytes - m_tcpClient->write(m_outBlock);
ui->clientStatusLabel->setText("服务端连接成功");
m_outBlock.resize(0);
}
void Widget::updateClientProgress(qint64 numBytes)
{
m_bytesWritten += (int)numBytes;
if(m_bytesToWrite > 0)
{
// 一次发送64*1024byte内容
m_outBlock = m_localFile->read(qMin(m_bytesToWrite,m_payloadSize));
m_bytesToWrite -= (int)m_tcpClient->write(m_outBlock);
m_outBlock.resize(0);
}
else
{
if(m_localFile->isOpen())
{
m_localFile->close();
}
}
int nPercent = (double)m_bytesWritten*100 / m_totalBytes ;
ui->clientProgressBar->setMaximum(100);
if(m_bytesWritten == m_totalBytes)
{
ui->clientProgressBar->setValue(100);
ui->clientStatusLabel->setText(QString("传送文件 %1 成功").arg(m_fileName));
if(m_localFile->isOpen())
{
m_localFile->close();
}
m_tcpClient->close();
}
else
{
ui->clientProgressBar->setValue(nPercent);
}
}
void Widget::displayError(QAbstractSocket::SocketError)
{
qDebug()<<m_tcpClient->errorString();
m_tcpClient->close();
ui->clientProgressBar->reset();
QString sError = m_tcpClient->errorString();
sError = QString("客户端出错:%1,请检查服务端是否连接正常").arg(sError);
ui->clientStatusLabel->setText(sError);
ui->sendButton->setEnabled(true);
}
void Widget::on_openButton_clicked()
{
ui->clientProgressBar->reset();
ui->clientStatusLabel->setText("等待打开文件!");
openFile();
}
void Widget::on_closeButton_clicked()
{
close();
}
void Widget::on_sendButton_clicked()
{
sendFile();
}

欧特克_Glodon
- 粉丝: 5w+
最新资源
- 数智引擎驱动:高校科技成果转化新模式探索.docx
- grib和grib2数据介绍
- java web 通过配置文件的配置简单解释过滤器执行流程
- windows10可用的wgrib2工具
- 【Android应用源码】WindowThemeSample.zip
- 【Android应用源码】Win8Style.zip
- 【Android应用源码】WordPress for Android.zip
- 【Android应用源码】Wind音乐播放器.zip
- 【Android应用源码】WordPress.zip
- 【Android应用源码】WS_SECURE_PAY.zip
- 【Android应用源码】XinLan_SlideMenu.zip
- 【Android应用源码】XListview实现上拉刷新下拉加载功能.zip
- 【Android应用源码】XListView--master.zip
- 【Android应用源码】XmlResourceParserSample.zip
- 【Android应用源码】XmlValuesSample.zip
- 【Android应用源码】XmlValuesSample2.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
- 3
- 4
- 5
- 6
前往页