feat: 实现 TcpServer 的部分功能函数
This commit is contained in:
parent
aa31886279
commit
d9ab714a12
@ -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<QHostAddress> 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();
|
||||
}
|
||||
|
@ -2,6 +2,10 @@
|
||||
#define EXTCPSERVER_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QLabel>
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
#include <QHostInfo>
|
||||
|
||||
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
|
||||
|
@ -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
|
||||
|
@ -102,15 +102,81 @@
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QToolBar" name="mainToolBar">
|
||||
<property name="toolButtonStyle">
|
||||
<enum>Qt::ToolButtonFollowStyle</enum>
|
||||
</property>
|
||||
<attribute name="toolBarArea">
|
||||
<enum>TopToolBarArea</enum>
|
||||
</attribute>
|
||||
<attribute name="toolBarBreak">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
<addaction name="actStart"/>
|
||||
<addaction name="actStop"/>
|
||||
<addaction name="actClear"/>
|
||||
<addaction name="actQuit"/>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusBar"/>
|
||||
<action name="actStart">
|
||||
<property name="icon">
|
||||
<iconset resource="images/resources.qrc">
|
||||
<normaloff>:/Image6.png</normaloff>:/Image6.png</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>开始监听</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>开始监听</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actStop">
|
||||
<property name="icon">
|
||||
<iconset resource="images/resources.qrc">
|
||||
<normaloff>:/Image3.png</normaloff>:/Image3.png</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>停止监听</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>停止监听</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actClear">
|
||||
<property name="icon">
|
||||
<iconset resource="images/resources.qrc">
|
||||
<normaloff>:/Image1.png</normaloff>:/Image1.png</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>清除</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>清除文本</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actQuit">
|
||||
<property name="icon">
|
||||
<iconset resource="images/resources.qrc">
|
||||
<normaloff>:/Image2.jpg</normaloff>:/Image2.jpg</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>退出</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>退出程序</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actHostInfo">
|
||||
<property name="text">
|
||||
<string>本机地址</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>获取本机地址</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<layoutdefault spacing="6" margin="11"/>
|
||||
<resources/>
|
||||
<resources>
|
||||
<include location="images/resources.qrc"/>
|
||||
</resources>
|
||||
<connections/>
|
||||
</ui>
|
||||
|
8
QtTcpEx/ExTcpServer/images/resources.qrc
Normal file
8
QtTcpEx/ExTcpServer/images/resources.qrc
Normal file
@ -0,0 +1,8 @@
|
||||
<RCC>
|
||||
<qresource prefix="/">
|
||||
<file>Image6.png</file>
|
||||
<file>Image3.png</file>
|
||||
<file>Image2.jpg</file>
|
||||
<file>Image1.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
Loading…
Reference in New Issue
Block a user