服务端具体代码如下:
.h
#include <QTcpServer>
#include <QTcpSocket>
#include <QFileDialog>
#include <QFile>
#define BUF_SIZE 1024*8
class ServerProject : public QWidget
{
Q_OBJECT
public:
ServerProject(QWidget *parent = nullptr);
~ServerProject();
void iniServer();
void closeServer();
void sendData();
private slots:
void on_listenBtn_clicked();
void on_sendBtn_clicked();
void on_clearRecBtn_clicked();
void on_selectBtn_clicked();
void on_sendFileBtn_clicked();
void newConnectionSlot();
void readyReadSlot();
private:
Ui::ServerProjectClass ui;
QTcpServer* m_server;
QTcpSocket* m_socket;
QString m_filename;
int m_fileSize, m_sendSize;
QFile m_file;
};
.cpp
ServerProject::ServerProject(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
setWindowTitle("Server");
iniServer();
}
ServerProject::~ServerProject()
{
closeServer();
if (m_socket != NULL) {
delete m_socket;
m_socket = NULL;
}
}
void ServerProject::iniServer() {
//m_sendEnable = false;
m_filename = "";
m_fileSize = 0;
m_sendSize = 0;
m_socket = NULL;
ui.clearRecBtn->setEnabled(false);
ui.sendBtn->setEnabled(false);
ui.selectBtn->setEnabled(false);
ui.sendFileBtn->setEnabled(false);
m_server = new QTcpServer(this);
}
void ServerProject::closeServer() {
m_server->close();
if (m_socket == NULL) {
return;
}
//断开与客户端的连接
if (m_socket->state() == QAbstractSocket::ConnectedState) {
m_socket->disconnectFromHost();
if (m_socket->state() != QAbstractSocket::UnconnectedState) {
m_socket->abort();
}
}
}
void ServerProject::on_listenBtn_clicked() {
if (m_server->isListening()) {
closeServer();
//关闭server后恢复界面状态
ui.listenBtn->setText("Listen");
ui.addressEt->setEnabled(true);
ui.portEt->setEnabled(true);
}
else {
//可以使用 QHostAddress::Any 监听所有地址的对应端口
const QString address_text = ui.addressEt->text();
const unsigned short port = ui.portEt->text().toUShort();
const QHostAddress address = (address_text == "Any")
? QHostAddress::Any
: QHostAddress(address_text);
//开始监听,并判断是否成功
if (m_server->listen(address, port)) {
//连接成功就修改界面按钮提示,以及地址栏不可编辑
ui.listenBtn->setText("Close");
ui.addressEt->setEnabled(false);
ui.portEt->setEnabled(false);
}
connect(m_server, &QTcpServer::newConnection, this, &ServerProject::newConnectionSlot);
}
}
void ServerProject::newConnectionSlot() {
if (m_server->hasPendingConnections())
{
//nextPendingConnection返回下一个挂起的连接作为已连接的QTcpSocket对象
//套接字是作为服务器的子级创建的,这意味着销毁QTcpServer对象时会自动删除该套接字。
//最好在完成处理后显式删除该对象,以避免浪费内存。
//返回的QTcpSocket对象不能从另一个线程使用,如有需要可重写incomingConnection().
m_socket = m_server->nextPendingConnection();
ui.receiveEt->append("connected.........");
connect(m_socket, &QTcpSocket::readyRead, this, &ServerProject::readyReadSlot);
ui.clearRecBtn->setEnabled(true);
ui.selectBtn->setEnabled(true);
ui.sendBtn->setEnabled(true);
}
}
void ServerProject::readyReadSlot() {
if (m_socket->bytesAvailable() <= 0)
return;
//注意收发两端文本要使用对应的编解码
const QString recv_text = QString::fromUtf8(m_socket->readAll());
if (ui.sendFileBtn->isEnabled()) {
ui.receiveEt->append(recv_text);
}
else {
if (recv_text == "FileHead Succ") {
qDebug() << "get FileHead Succ--------------";
sendData();
}
else if (recv_text=="FileRec Done") {
m_file.close();
ui.sendBtn->setEnabled(true);
ui.selectBtn->setEnabled(true);
ui.sendFileBtn->setEnabled(true);
}
}
}
void ServerProject::on_sendBtn_clicked() {
if (!m_server->isListening())
return;
//将发送区文本发送给客户端
const QByteArray send_data = ui.sendEt->toPlainText().toUtf8();
//数据为空就返回
if (send_data.isEmpty())
return;
m_socket->write(send_data);
ui.sendEt->clear();
}
void ServerProject::on_clearRecBtn_clicked() {
ui.receiveEt->clear();
}
void ServerProject::on_selectBtn_clicked() {
QString filePath = QFileDialog::getOpenFileName(this, "open", "../");
if (!filePath.isEmpty()) {
//m_sendEnable = false;
m_filename = "";
m_fileSize = 0;
m_sendSize = 0;
QFileInfo info(filePath);
m_filename = info.fileName();
m_fileSize = info.size();
qDebug() << "slectfile name: " << m_filename << " size: " << m_fileSize;
m_file.setFileName(filePath);
if (m_file.open(QIODevice::ReadOnly)) {
//m_sendEnable = true;
ui.selectBtn->setEnabled(false);
ui.sendBtn->setEnabled(false);
ui.sendFileBtn->setEnabled(true);
}
}
}
void ServerProject::on_sendFileBtn_clicked() {
QString filehead = QString("%1#%2").arg(m_filename).arg(m_fileSize);
qint64 len = m_socket->write(filehead.toUtf8());
if (len > 0) {
qDebug() << "filehead: " << filehead;
ui.sendFileBtn->setEnabled(false);
}
}
void ServerProject::sendData() {
qint64 len = 0;
do {
//一次发送的大小
char buf[BUF_SIZE] = { 0 };
len = 0;
len = m_file.read(buf, BUF_SIZE); //读取的字节数
len = m_socket->write(buf, len); //发送的字节数
qDebug() << "len-----" << len;
//已发数据累加
m_sendSize += len;
} while (len > 0);
}
服务端代码如下:
ClientPro::ClientPro(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
initClient();
}
ClientPro::~ClientPro()
{
qDebug() << "~ClientPro()----------------------------";
if (m_client->state() == QAbstractSocket::ConnectedState) {
//如果使用disconnectFromHost()不会重置套接字,isValid还是会为true
m_client->abort();
}
}
void ClientPro::initClient() {
m_fileMaxsize = 100;
m_sendEnable = false;
//创建client对象
m_client = new QTcpSocket(this);
}
void ClientPro::on_connectBtn_clicked() {
if (m_client->state() == QAbstractSocket::ConnectedState) {
//如果使用disconnectFromHost()不会重置套接字,isValid还是会为true
m_client->abort();
}
else if (m_client->state() == QAbstractSocket::UnconnectedState) {
//从界面上读取ip和端口
const QHostAddress address = QHostAddress(ui.addressEt->text());
const unsigned short port = ui.portEt->text().toUShort();
//连接服务器
m_client->connectToHost(address, port);
}
else {
ui.receiveEt->append("It is not ConnectedState or UnconnectedState");
}
connect(m_client, &QTcpSocket::connected, this, &ClientPro::connectedSlot);
connect(m_client, &QTcpSocket::disconnected, this, &ClientPro::disconnectedSlot);
connect(m_client, &QTcpSocket::readyRead, this, &ClientPro::readyReadSlot);
}
void ClientPro::connectedSlot() {
ui.connectBtn->setText("Disconnect");
ui.addressEt->setEnabled(false);
ui.portEt->setEnabled(false);
}
void ClientPro::disconnectedSlot() {
ui.connectBtn->setText("Connect");
ui.addressEt->setEnabled(true);
ui.portEt->setEnabled(true);
}
void ClientPro::readyReadSlot() {
if (m_client->bytesAvailable() <= 0)
return;
//注意收发两端文本要使用对应的编解码
QByteArray buf = m_client->readAll();
const QString recv_text = QString::fromUtf8(buf);
QStringList list = recv_text.split("#");
qDebug() << "listsize: " << list.size();
if (!m_sendEnable&&list.size() == 2) {
m_sendEnable = true;
ui.sendBtn->setEnabled(false);
m_recSize = 0;
m_client->write("FileHead Succ");
m_file.setFileName(list[0]);
m_fileMaxsize = list[1].toInt();
ui.progressBar->setValue(m_recSize);
ui.progressBar->setMaximum(m_fileMaxsize);
if (m_file.open(QIODevice::WriteOnly)) {
return;
}
}
if (!m_sendEnable) {
ui.receiveEt->append(recv_text);
}
else {
qint64 len = m_file.write(buf);
m_recSize += len;
qDebug() << "client----m_recSize: " << m_recSize;
ui.progressBar->setValue(m_recSize);
if (m_recSize == m_fileMaxsize) {
qDebug() << "rec done--------------";
m_sendEnable = false;
m_file.close();
m_client->write("FileRec Done");
ui.sendBtn->setEnabled(true);
}
}
}
void ClientPro::on_sendBtn_clicked() {
if (!m_client->isValid())
return;
//将发送区文本发送给客户端
const QByteArray send_data = ui.sendEt->toPlainText().toUtf8();
//数据为空就返回
if (send_data.isEmpty())
return;
m_client->write(send_data);
}
void ClientPro::on_clearRecBtn_clicked() {
ui.receiveEt->clear();
}
void ClientPro::on_clearSendBtn_clicked() {
ui.sendEt->clear();
}
动态显示如下:
版权声明:本文为weixin_41377572原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。