#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFileDialog"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
loadSize = 4*1024;
totalBytes = 0;
bytesReceived = 0;
fileNameSize = 0;
tcpClient = new QTcpSocket(this);
connect(tcpClient,SIGNAL(readyRead()),this,SLOT(updateServerProgress()));
connect(tcpClient,SIGNAL(connected()),this,SLOT(startTransfer()));
connect(tcpClient,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
// ui->sendButton->setEnabled(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openFile()
{
fileName = QFileDialog::getOpenFileName(this);
if(!fileName.isEmpty())
{
ui->sendButton->setEnabled(true);
ui->clientStatusLabel->setText(tr("打开文件 %1 成功").arg(fileName));
}
qDebug() << fileName;
}
void MainWindow::startTransfer()
{
fileNameSize = 0;
bytesReceived =0;
}
void MainWindow::send() //连接到服务器,执行发送
{
ui->sendButton->setEnabled(false);
ui->clientStatusLabel->setText(tr("连接中…"));
tcpClient->connectToHost(ui->hostLineEdit->text(),ui->portLineEdit->text().toInt());//连接
}
void MainWindow::acceptConnection() //接受连接
{
tcpClient = tcpServer.nextPendingConnection();
connect(tcpClient,SIGNAL(bytesWritten(qint64)),this, SLOT(updateClientProgress(qint64)));
connect(tcpClient,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
ui->clientStatusLabel->setText(tr("接受连接"));
}
void MainWindow::updateServerProgress() //更新进度条,接收数据
{
QDataStream in(tcpClient);
in.setVersion(QDataStream::Qt_4_6);
qDebug() << "fileNameSize = " << fileNameSize;
if(bytesReceived <= sizeof(qint64)*2)
{ //如果接收到的数据小于16个字节,那么是刚开始接收数据,我们保存到//来的头文件信息
if((tcpClient->bytesAvailable() >= sizeof(qint64)*2) && (fileNameSize == 0))
{ //接收数据总大小信息和文件名大小信息
in >> totalBytes >> fileNameSize;
bytesReceived += sizeof(qint64) * 2;
}
if((tcpClient->bytesAvailable() >= fileNameSize) && (fileNameSize != 0))
{ //接收文件名,并建立文件
in >> fileName;
ui->clientStatusLabel->setText(tr("接收文件 %1 …").arg(fileName));
bytesReceived += fileNameSize;
localFile = new QFile(fileName);
qDebug() << fileName;
if(!localFile->open(QFile::WriteOnly))
{
qDebug() << "open file error!";
return;
}
}
else return;
}
if(bytesReceived < totalBytes)
{ //如果接收的数据小于总数据,那么写入文件
bytesReceived += tcpClient->bytesAvailable();
inBlock = tcpClient->readAll();
localFile->write(inBlock);
inBlock.resize(0);
}
ui->clientProgressBar->setMaximum(totalBytes);
ui->clientProgressBar->setValue(bytesReceived);
//更新进度条
if(bytesReceived == totalBytes)
{ //接收数据完成时
// tcpServerConnection->close();
localFile->close();
ui->clientStatusLabel->setText(tr("接收文件 %1 成功!").arg(fileName));
}
}
void MainWindow::displayError(QAbstractSocket::SocketError) //显示错误
{
qDebug() << tcpClient->errorString();
tcpClient->close();
ui->clientProgressBar->reset();
ui->clientStatusLabel->setText(tr("客户端就绪"));
ui->sendButton->setEnabled(true);
}
void MainWindow::on_sendButton_clicked()
{
send();
}
- 1
- 2
前往页