源码下载点这
1、简介
界面如上,近期研究了一下,参考网上别人程序自己做了修改,没有用多线程所以在传输时候有时候会出现卡顿,该程序只适合传小文件,因为定义文件大小是quint32类型,理论上只能传4G以下的文件,文件保存路径程序中有显示
2、操作流程
1、监听 2、连接 3、打开文件 4、发送 文件保存在Receive目录下
3、源码
#ifndef UPDATACLIENT_H
#define UPDATACLIENT_H
#include <QWidget>
#include <QTcpSocket>
#include <QFile>
#include <QString>
namespace Ui {
class UpdataClient;
}
class UpdataClient : public QWidget
{
Q_OBJECT
public:
explicit UpdataClient(QWidget *parent = nullptr);
~UpdataClient();
public slots:
void ClientReadError(QAbstractSocket::SocketError);
void ClinetReadData();
private slots:
void on_butConnect_clicked();
void on_buttonSend_clicked();
void on_buttonSelect_clicked();
private:
Ui::UpdataClient *ui;
quint16 port;
QString ip;
QTcpSocket* m_tcpSocket;
QFile *m_file;
QString fileName;
quint32 fileSize;
quint32 fileLoad;
bool socketFlag;
};
#endif
#include "updataclient.h"
#include "ui_updataclient.h"
#include <QFileDialog>
#include <QFileInfo>
#include <string.h>
#include <QThread>
UpdataClient::UpdataClient(QWidget *parent) :
QWidget(parent),
ui(new Ui::UpdataClient)
{
ui->setupUi(this);
port = 8888;
ip = "192.168.1.250";
m_tcpSocket = nullptr;
this->m_tcpSocket=new QTcpSocket(this);
socketFlag = false;
ui->progressBar->setValue(0);
ui->buttonSend->setEnabled(false);
ui->buttonSelect->setEnabled(false);
m_file = new QFile(this);
}
UpdataClient::~UpdataClient()
{
if(m_file->isOpen()==true)
{
m_file->close();
m_file->deleteLater();
}
if(socketFlag == true)
{
m_tcpSocket->disconnect();
m_tcpSocket->abort();
m_tcpSocket->close();
m_tcpSocket->deleteLater();
}
delete ui;
}
void UpdataClient::on_butConnect_clicked()
{
if(socketFlag == false)
{
m_tcpSocket->connectToHost(ip,port);
connect(m_tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(ClientReadError(QAbstractSocket::SocketError)));
if (m_tcpSocket->waitForConnected(1000))
{
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(ClinetReadData()));
socketFlag = true;
ui->buttonSelect->setEnabled(true);
ui->TcpMessage->append("已连接服务器"+QString("[%1:%2]").arg(ip).arg(port));
ui->butConnect->setText("断开");
}
else
{
m_tcpSocket->disconnect();
m_tcpSocket->close();
socketFlag=false;
ui->buttonSend->setEnabled(false);
ui->buttonSelect->setEnabled(false);
ui->TcpMessage->append("连接失败"+QString("[%1:%2]").arg(ip).arg(port));
ui->butConnect->setText("连接");
}
}
else if(socketFlag== true)
{
m_tcpSocket->disconnect();
m_tcpSocket->abort();
m_tcpSocket->close();
socketFlag=false;
ui->TcpMessage->append("已断开服务器"+QString("[%1:%2]\r\n").arg(ip).arg(port));
ui->butConnect->setText("连接");
ui->buttonSend->setEnabled(false);
ui->buttonSelect->setEnabled(false);
}
}
void UpdataClient::ClinetReadData()
{
QByteArray ba = m_tcpSocket->readAll();
QString data;
data=QString::fromLocal8Bit(ba);
ui->TcpMessage->insertPlainText(data);
ui->TcpMessage->moveCursor(QTextCursor::End);
}
void UpdataClient::ClientReadError(QAbstractSocket::SocketError)
{
QString err=QString("发生错误:%1").arg(m_tcpSocket->errorString());
ui->TcpMessage->append(err);
m_tcpSocket->disconnect();
m_tcpSocket->close();
socketFlag=false;
ui->buttonSend->setEnabled(false);
ui->buttonSelect->setEnabled(false);
ui->TcpMessage->append("已断开服务器"+QString("[%1:%2]").arg(ip).arg(port));
ui->butConnect->setText("连接");
}
void UpdataClient::on_buttonSend_clicked()
{
unsigned char tempStr[4*1024];
memset(tempStr,0,4*1024);
tempStr[0]='#';
tempStr[1]='#';
tempStr[2]='#';
tempStr[3]=static_cast<unsigned char>(fileSize>>24);
tempStr[4]=static_cast<unsigned char>(fileSize>>16);
tempStr[5]=static_cast<unsigned char>(fileSize>>8);
tempStr[6]=static_cast<unsigned char>(fileSize);
tempStr[7]='#';
QByteArray ba;
char *pstr = nullptr;
ba = fileName.toUtf8();
pstr = ba.data();
memcpy(tempStr+8,pstr,static_cast<size_t>(ba.size()));
m_tcpSocket->write(reinterpret_cast<char *>(tempStr),4*1024);
m_tcpSocket->flush();
qint64 len = 0;
fileLoad = 0;
do{
char buf[4*1024] = {0};
len = 0;
len = m_file->read(buf, sizeof(buf));
len = m_tcpSocket->write(buf, len);
fileLoad += len;
ui->progressBar->setValue(fileLoad/1024);
}while (len > 0);
m_file->close();
ui->buttonSelect->setEnabled(true);
ui->buttonSend->setEnabled(false);
}
void UpdataClient::on_buttonSelect_clicked()
{
QString filePath = QFileDialog::getOpenFileName(this, "Open", "../");
if(!filePath.isEmpty())
{
QFileInfo info(filePath);
fileName = info.fileName();
fileSize = static_cast<quint32>(info.size());
m_file->setFileName(filePath);
if(m_file->open(QIODevice::ReadOnly))
{
ui->TcpMessage->append(QString("FileName:%1 \n\nFileSize:%2 KB").arg(fileName).arg(fileSize/1024));
ui->buttonSend->setEnabled(true);
ui->buttonSelect->setEnabled(false);
ui->progressBar->setMaximum(fileSize/1024);
ui->progressBar->setMinimum(0);
}
else
{
qDebug()<<"打开文件失败";
}
}
else
{
qDebug()<<"文件路径出错";
}
}
#ifndef UPDATASERVER_H
#define UPDATASERVER_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QList>
namespace Ui {
class UpdataServer;
}
class UpdataServer : public QWidget
{
Q_OBJECT
public:
explicit UpdataServer(QWidget *parent = nullptr);
~UpdataServer();
private:
Ui::UpdataServer *ui;
QTcpServer *tcpServer;
QList<QTcpSocket*> tcpClientList;
QTcpSocket *tcpSocket;
quint16 port;
QString ip;
bool FileFlag;
QString fileName;
QString fileDir;
quint32 fileSize;
quint32 fileLoad;
QFile file;
public slots:
void NewConnection();
void disconnectedSlot();
void ReadyRead();
private slots:
void on_pushButton_connect_clicked();
};
#endif
#include "updataserver.h"
#include "ui_updataserver.h"
#include <string>
#include <QDir>
#include <QCoreApplication>
UpdataServer::UpdataServer(QWidget *parent) :
QWidget(parent),
ui(new Ui::UpdataServer)
{
ui->setupUi(this);
port = 8888;
ip = "192.168.1.250";
tcpSocket = nullptr;
tcpServer = nullptr;
tcpServer = new QTcpServer(this);
QDir dir;
if(!dir.exists("./Receive"))
{
dir.mkpath("./Receive");
}
ui->progressBar->setValue(0);
}
UpdataServer::~UpdataServer()
{
if(tcpServer->isListening()==true)
{
if(tcpSocket != nullptr)
{
for(int i=tcpClientList.length()-1;i>=0;i--)
{
tcpClientList.at(i)->disconnectFromHost();
}
if(tcpClientList.isEmpty()==true)
{
tcpServer->disconnect();
tcpServer->close();
tcpServer->deleteLater();
tcpSocket=nullptr;
}
}
}
if(file.isOpen()==true)
{
file.close();
}
delete ui;
}
void UpdataServer::on_pushButton_connect_clicked()
{
QString temp;
if(tcpServer->isListening()==true)
{
if(tcpSocket != nullptr)
{
for(int i=tcpClientList.length()-1;i>=0;i--)
{
tcpClientList.at(i)->disconnectFromHost();
}
if(tcpClientList.isEmpty()==true)
{
tcpServer->close();
tcpSocket=nullptr;
ui->textEdit_GetText->append("停止监听成功");
ui->pushButton_connect->setText("监听");
disconnect(tcpServer,&QTcpServer::newConnection,this,&UpdataServer::NewConnection);
}
}
else
{
QString str = "当前没有连接,可重新监听";
tcpServer->close();
ui->textEdit_GetText->append(str);
ui->pushButton_connect->setText("监听");
disconnect(tcpServer,&QTcpServer::newConnection,this,&UpdataServer::NewConnection);
}
}
else
{
if(true == tcpServer->listen(QHostAddress(ip),port))
{
ui->textEdit_GetText->append("开始监听");
ui->pushButton_connect->setText("停止");
FileFlag = false;
connect(tcpServer,&QTcpServer::newConnection,this,&UpdataServer::NewConnection);
}
else
{
ui->textEdit_GetText->append("监听失败");
return ;
}
}
}
void UpdataServer::NewConnection()
{
tcpSocket = tcpServer->nextPendingConnection();
QString cip = tcpSocket->peerAddress().toString();
quint16 prot = tcpSocket->peerPort();
QString temp= QString("%1.%2:成功连接").arg(cip).arg(prot);
ui->textEdit_GetText->append(temp);
temp=temp.split(":")[0];
tcpClientList.append(tcpSocket);
connect(tcpSocket,&QTcpSocket::readyRead,this,&UpdataServer::ReadyRead);
connect(tcpSocket,&QTcpSocket::disconnected,this,&UpdataServer::disconnectedSlot);
}
void UpdataServer::disconnectedSlot()
{
tcpSocket = static_cast<QTcpSocket *>(this->sender());
QString temp =QString("%1.%2:退出").arg(tcpSocket->peerAddress().toString()).arg(tcpSocket->peerPort());
ui->textEdit_GetText->append(temp);
temp=temp.split(":")[0];
for(int i=0;i<tcpClientList.length();i++)
{
if(tcpClientList.at(i)->peerAddress() == tcpSocket->peerAddress())
{
if(tcpClientList.at(i)->peerPort() == tcpSocket->peerPort())
{
tcpClientList.removeAt(i);
break;
}
}
}
}
void UpdataServer::ReadyRead()
{
unsigned char tempStr[4*1024];
memset(tempStr,0,sizeof(tempStr));
tcpSocket = static_cast<QTcpSocket *>(this->sender());
QByteArray array = tcpSocket->readAll();
memcpy(tempStr,array.data(),4*1024);
if(tempStr[0]=='#'&&tempStr[1]=='#'&&tempStr[2]=='#'&&tempStr[7]=='#'&&FileFlag == false)
{
fileSize =static_cast<quint32>(tempStr[3]<<24|tempStr[4]<<16|tempStr[5]<<8|tempStr[6]);
fileLoad = 0;
fileName = "./Receive/"+QString(reinterpret_cast<char *>(&tempStr[8]));
ui->progressBar->setMaximum(fileSize/1024);
ui->progressBar->setMinimum(0);
file.setFileName(fileName);
if(!file.open(QIODevice::WriteOnly))
{
qDebug()<<"以只写方式打开文件失败";
tcpSocket->disconnectFromHost();
return ;
}
else
{
FileFlag = true;
}
}
else if(FileFlag == true)
{
qint64 len = file.write(array);
if(len > 0)
{
fileLoad += len;
}
ui->progressBar->setValue(fileLoad/1024);
if(fileLoad >= fileSize)
{
ui->textEdit_GetText->append("文件接收完成");
fileLoad=0;
file.close();
FileFlag = false;
fileDir = QCoreApplication::applicationDirPath()+QString(fileName).section(".", 1, 2);
ui->textEdit_GetText->append(QString("FileDir:\n%1\n\nFileSize:%2 KB").arg(fileDir).arg(fileSize/1024));
}
}
}
|