From d9ab714a127f846d459c333b6f2312ceff777d68 Mon Sep 17 00:00:00 2001 From: touwoyimuli Date: Fri, 22 Nov 2019 00:12:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=20TcpServer=20?= =?UTF-8?q?=E7=9A=84=E9=83=A8=E5=88=86=E5=8A=9F=E8=83=BD=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QtTcpEx/ExTcpServer/ExTcpServer.cpp | 91 ++++++++++++++++++++++++ QtTcpEx/ExTcpServer/ExTcpServer.h | 34 +++++++++ QtTcpEx/ExTcpServer/ExTcpServer.pro | 11 ++- QtTcpEx/ExTcpServer/ExTcpServer.ui | 68 +++++++++++++++++- QtTcpEx/ExTcpServer/images/resources.qrc | 8 +++ 5 files changed, 205 insertions(+), 7 deletions(-) create mode 100644 QtTcpEx/ExTcpServer/images/resources.qrc diff --git a/QtTcpEx/ExTcpServer/ExTcpServer.cpp b/QtTcpEx/ExTcpServer/ExTcpServer.cpp index ec97be3..4c0a2f1 100644 --- a/QtTcpEx/ExTcpServer/ExTcpServer.cpp +++ b/QtTcpEx/ExTcpServer/ExTcpServer.cpp @@ -12,3 +12,94 @@ ExTcpServer::~ExTcpServer() { delete ui; } + +void ExTcpServer::closeEvent(QCloseEvent *event) //关闭窗口时候停止监听 +{ + if (m_tcpServer->isListening()) + m_tcpServer->close(); + + event->accept(); +} + +void ExTcpServer::on_actStart_triggered() +{ + QString Ip = ui->comboBox->currentText(); + quint16 port = ui->spinBox->value(); + + QHostAddress addr(Ip); + m_tcpServer->listen(addr, port); //监听指定的 IP 和指定的 port + ui->plainTextEdit->appendPlainText("服务器地址为:" + m_tcpServer->serverAddress().toString() + " 服务器端口:" + QString::number(m_tcpServer->serverPort())); + ui->plainTextEdit->appendPlainText("开始监听..."); + + ui->actStart->setEnabled(false); + ui->actStop->setEnabled(true); + m_labListen->setText("监听状态:正在监听..."); +} + +void ExTcpServer::on_actStop_triggered() +{ + if (!m_tcpServer->isListening()) + return; + + m_tcpServer->close(); //停止监听 + + ui->actStart->setEnabled(true); + ui->actStop->setEnabled(false); + m_labListen->setText("监听状态:监听已经停止"); +} + +void ExTcpServer::on_actClear_triggered() +{ + ui->plainTextEdit->clear(); +} + +void ExTcpServer::on_actQuit_triggered() +{ + close(); +} + +void ExTcpServer::on_btnSend_clicked() +{ + +} + + + +void ExTcpServer::on_actHostInfo_triggered() +{ + QString hostName = QHostInfo::localHostName(); + QHostInfo hostInfo = QHostInfo::fromName(hostName); + ui->plainTextEdit->appendPlainText("本机主机名:" + hostName); + + QList list = hostInfo.addresses(); + + if (list.empty()) + return; + + foreach (QHostAddress addr, list) { + if (addr.protocol() == QAbstractSocket::IPv4Protocol) { + ui->plainTextEdit->appendPlainText("本机 IP 地址:" + addr.toString()); + + if (ui->comboBox->findText(addr.toString()) < 0) //下拉列表没有的就添加进去 + ui->comboBox->addItem(addr.toString()); + } + } +} + +void ExTcpServer::onSocketReadyRead() +{ + +} + +void ExTcpServer::onClientConnected() //客户端连接时 +{ + ui->plainTextEdit->appendPlainText("客户端套接字连接\n对等(peer)地址:" + m_tcpSocket->peerAddress().toString() + + " 对等(peer)端口:" + QString::number(m_tcpSocket->peerPort())); + +} + +void ExTcpServer::onClientDisonnected() //客户端断开连接时 +{ + ui->plainTextEdit->appendPlainText("客户端套接字断开"); + m_tcpSocket->deleteLater(); +} diff --git a/QtTcpEx/ExTcpServer/ExTcpServer.h b/QtTcpEx/ExTcpServer/ExTcpServer.h index edd2700..4823edb 100644 --- a/QtTcpEx/ExTcpServer/ExTcpServer.h +++ b/QtTcpEx/ExTcpServer/ExTcpServer.h @@ -2,6 +2,10 @@ #define EXTCPSERVER_H #include +#include +#include +#include +#include namespace Ui { class ExTcpServer; @@ -15,8 +19,38 @@ public: explicit ExTcpServer(QWidget *parent = nullptr); ~ExTcpServer(); +private: + QString getLocalIp(); //获取本机 IP + +protected: + void closeEvent(QCloseEvent* event); + + + +private slots: +//UI的槽函数 + void on_actStart_triggered(); //开始监听 + void on_actStop_triggered(); //停止监听 + void on_actClear_triggered(); //清除文本框内容 + void on_actQuit_triggered(); //退出程序 + void on_btnSend_clicked(); //发送消息 + void on_actHostInfo_triggered(); //获取本机的 name 和 IP + +//自定义的槽函数 + void onSocketReadyRead(); //读取 socket 传入时候的数据 + void onClientConnected(); //client socket conneted + void onClientDisonnected(); //client socket disconneted + + private: Ui::ExTcpServer *ui; + + QLabel* m_labListen; + QLabel* m_labSocket; + QTcpServer* m_tcpServer; + QTcpSocket* m_tcpSocket; + + }; #endif // EXTCPSERVER_H diff --git a/QtTcpEx/ExTcpServer/ExTcpServer.pro b/QtTcpEx/ExTcpServer/ExTcpServer.pro index 359cd8d..878063f 100644 --- a/QtTcpEx/ExTcpServer/ExTcpServer.pro +++ b/QtTcpEx/ExTcpServer/ExTcpServer.pro @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += core gui +QT += core gui network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -51,8 +51,7 @@ qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target -DISTFILES += \ - images/Image2.jpg \ - images/Image1.png \ - images/Image3.png \ - images/Image6.png +DISTFILES += + +RESOURCES += \ + images/resources.qrc diff --git a/QtTcpEx/ExTcpServer/ExTcpServer.ui b/QtTcpEx/ExTcpServer/ExTcpServer.ui index bb4c7b7..c8d538a 100644 --- a/QtTcpEx/ExTcpServer/ExTcpServer.ui +++ b/QtTcpEx/ExTcpServer/ExTcpServer.ui @@ -102,15 +102,81 @@ + + Qt::ToolButtonFollowStyle + TopToolBarArea false + + + + + + + + + :/Image6.png:/Image6.png + + + 开始监听 + + + 开始监听 + + + + + + :/Image3.png:/Image3.png + + + 停止监听 + + + 停止监听 + + + + + + :/Image1.png:/Image1.png + + + 清除 + + + 清除文本 + + + + + + :/Image2.jpg:/Image2.jpg + + + 退出 + + + 退出程序 + + + + + 本机地址 + + + 获取本机地址 + + - + + + diff --git a/QtTcpEx/ExTcpServer/images/resources.qrc b/QtTcpEx/ExTcpServer/images/resources.qrc new file mode 100644 index 0000000..6533853 --- /dev/null +++ b/QtTcpEx/ExTcpServer/images/resources.qrc @@ -0,0 +1,8 @@ + + + Image6.png + Image3.png + Image2.jpg + Image1.png + +