#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTime>
#include <QFileDialog>
#include <QMessageBox>
/*
文件发送客户端
日期:2020-11-16
*/
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_CntSocket = new QTcpSocket();
//收到数据
connect(m_CntSocket, &QTcpSocket::readyRead, this, &MainWindow::RecvData);
//发送数据
connect(m_CntSocket, SIGNAL(bytesWritten(qint64)), this, SLOT(refreshProgress(qint64)));
//断开连接
connect(m_CntSocket,SIGNAL(disconnected()), this, SLOT(disconnect()));
m_PayloadSize = 64 * 1024;
}
MainWindow::~MainWindow()
{
if (m_CntSocket->isOpen())
{
m_CntSocket->disconnectFromHost();
}
delete m_CntSocket;
delete ui;
}
void MainWindow::on_btnAddFile_clicked()
{
ui->btnAddFile->setEnabled(false);
QString PathFile = QFileDialog::getOpenFileName(this);
if(!PathFile.isEmpty())
{
ui->EdtFileList->append(PathFile);
}
ui->btnAddFile->setEnabled(true);
}
void MainWindow::on_btnClearFile_clicked()
{
ui->EdtFileList->clear();
//当前文件的进度条
ui->progressBar->setMaximum(100);
ui->progressBar->setValue(0);
//总进度条
ui->TotalprogressBar->setMaximum(100);
ui->TotalprogressBar->setValue(0);
}
void MainWindow::AppendLog(QString value)
{
qDebug()<<value;
}
void MainWindow::wait(int msec)
{
QTime _Timer = QTime::currentTime().addMSecs(msec);
while(QTime::currentTime() < _Timer)
{
QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
}
}
void MainWindow::on_btnSend_clicked()
{
if (ui->EdtFileList->toPlainText() =="")
{
QMessageBox::information(0, tr("提示"), tr("发送的文件不能为空!"));
return ;
}
if (m_CntSocket->isOpen())
{
m_CntSocket->disconnectFromHost();
}
QString ip=ui->edtIp->text();
int port=ui->edtPort->text().toInt();
m_CntSocket->connectToHost(ip,port);
if (!m_CntSocket->waitForConnected(3000))
{
m_CntSocket->disconnectFromHost();
AppendLog(tr("未连接上服务器!"));
QMessageBox::information(0, tr("提示"), tr("未连接上服务器!"));
return ;
}
/*************开始发送数据*******************/
ui->btnSend->setEnabled(false);
m_list = ui->EdtFileList->toPlainText().split("\n");
m_AllFileBytes=0;
m_AllSendBytes=0;
//打开文件
for (int i=0;i<=m_list.count()-1;i++)
{
m_PathFile=m_list[i];
m_LocPathFile = new QFile(m_PathFile);
m_LocPathFile->open((QFile::ReadOnly));
//计算所有文件的发送字节数
m_AllFileBytes += m_LocPathFile->size(); //文件的大小
QDataStream sendOut(&m_OutputBlock, QIODevice::WriteOnly);
sendOut.setVersion(QDataStream::Qt_DefaultCompiledVersion);
m_curFile = m_PathFile.right(m_PathFile.size() - m_PathFile.lastIndexOf('/') - 1);
sendOut << qint64(0) << qint64(0) << m_curFile; //总节数,文件名大小,文件名
m_AllFileBytes += m_OutputBlock.size();
m_OutputBlock.resize(0);
}
m_row=-1;
m_StartNext=true;
while (m_row<m_list.count()-1)
{
if (m_StartNext)
{
m_StartNext=false;
m_row++;
m_SendBytes=0;
m_TotalBytes=0;
m_BytesTobeSend=0;
m_PathFile=m_list[m_row];
m_LocPathFile = new QFile(m_PathFile);
m_LocPathFile->open((QFile::ReadOnly));
m_TotalBytes = m_LocPathFile->size(); //当前发送的总字节数
QDataStream sendOut(&m_OutputBlock, QIODevice::WriteOnly);
sendOut.setVersion(QDataStream::Qt_DefaultCompiledVersion);
m_curFile = m_PathFile.right(m_PathFile.size() - m_PathFile.lastIndexOf('/') - 1);
sendOut << qint64(0) << qint64(0) << m_curFile; //总节数,文件名大小,文件名
m_TotalBytes += m_OutputBlock.size();
sendOut.device()->seek(0); //移到开始处
sendOut << m_TotalBytes << qint64((m_OutputBlock.size() - sizeof(qint64) * 2));
m_BytesTobeSend = m_TotalBytes - m_CntSocket->write(m_OutputBlock); //
m_OutputBlock.resize(0);
AppendLog(tr("正在传输文件:")+m_curFile);
ui->label_currfilename->setText(tr("正在传输文件:")+m_curFile);
}
wait(200);
}
ui->label_currfilename->setText(tr("完成传输文件"));
}
void MainWindow::refreshProgress(qint64 bynum)
{
qApp->processEvents();
m_SendBytes+= (int)bynum; //当前文件发送的字节数
m_AllSendBytes+= (int)bynum;//所有文件发送的字节数
if (m_BytesTobeSend > 0) //文件还有未发送的字节数
{
m_OutputBlock = m_LocPathFile->read(qMin(m_BytesTobeSend, m_PayloadSize));
m_BytesTobeSend -= (int)m_CntSocket->write(m_OutputBlock);
m_OutputBlock.resize(0);
} else {
m_LocPathFile->close();
}
//当前文件的进度条
ui->progressBar->setMaximum(m_TotalBytes);
ui->progressBar->setValue(m_SendBytes);
//总进度条
ui->TotalprogressBar->setMaximum(m_AllFileBytes);
ui->TotalprogressBar->setValue(m_AllSendBytes);
if(m_SendBytes >= m_TotalBytes)
{
AppendLog(m_curFile+tr("传输完成!"));
m_LocPathFile->close();
ui->btnSend->setEnabled(true);
m_StartNext=true;
}
}
void MainWindow:: RecvData()
{
}
void MainWindow:: disconnect()
{
}
void MainWindow::on_btnCancel_clicked()
{
m_LocPathFile->close();
m_CntSocket->close();
AppendLog(m_curFile+tr("取消传输文件!"));
ui->btnSend->setEnabled(true);
ui->progressBar->reset();
ui->TotalprogressBar->reset();
}
- 1
- 2
前往页