From 674ee71e87fa1052857557ffd04938740d36d8ea Mon Sep 17 00:00:00 2001 From: touwoyimuli Date: Sun, 24 Nov 2019 01:18:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20TcpClient=20=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90=EF=BC=8C=E4=B8=94=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=92=8C=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E4=B9=8B=E9=97=B4=E7=9A=84=E9=80=9A=E8=AE=AF=E6=88=90?= =?UTF-8?q?=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QtTcpEx/ExTcpClient/ExTcpClient.cpp | 97 +++++++++++++++++++++++++++-- QtTcpEx/ExTcpClient/ExTcpClient.h | 9 +++ QtTcpEx/ExTcpClient/ExTcpClient.ui | 1 + QtTcpEx/ExTcpServer/ExTcpServer.cpp | 5 +- QtTcpEx/ExTcpServer/ExTcpServer.ui | 6 +- 5 files changed, 109 insertions(+), 9 deletions(-) diff --git a/QtTcpEx/ExTcpClient/ExTcpClient.cpp b/QtTcpEx/ExTcpClient/ExTcpClient.cpp index d67638d..e37f70f 100644 --- a/QtTcpEx/ExTcpClient/ExTcpClient.cpp +++ b/QtTcpEx/ExTcpClient/ExTcpClient.cpp @@ -6,6 +6,21 @@ ExTcpClient::ExTcpClient(QWidget *parent) : ui(new Ui::ExTcpClient) { ui->setupUi(this); + + m_labSocket = new QLabel("socket状态:"); + m_labSocket->setMidLineWidth(150); + ui->statusBar->addWidget(m_labSocket); + + m_tcpSocket = new QTcpSocket(this); + + QString localIp = getLocalIp(); + this->setWindowTitle(windowTitle() + "----本机IP:" + localIp); + ui->comboBox->addItem(localIp); + + connect(m_tcpSocket, SIGNAL(connected()), this, SLOT(onConnected())); + connect(m_tcpSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected())); + connect(m_tcpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onSocketStateChange(QAbstractSocket::SocketState))); + connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(onSocketReadyRead())); } ExTcpClient::~ExTcpClient() @@ -21,23 +36,85 @@ QString ExTcpClient::getLocalIp() QString localIp; foreach (QHostAddress addr, hostInfo.addresses()) { - if (QAbstractSocket::IPv4Protocol == addr.toString()) { + if (QAbstractSocket::IPv4Protocol == addr.protocol()) { localIp = addr.toString(); break; } } - return localIp; } +void ExTcpClient::closeEvent(QCloseEvent *event) +{ + if (m_tcpSocket->state() == QAbstractSocket::ConnectedState) + m_tcpSocket->disconnectFromHost(); + + event->accept(); +} + +void ExTcpClient::onConnected() +{ + ui->plainTextEdit->appendPlainText("已经连接到服务器\n客户端套接字连接\n对等(peer)地址:" + m_tcpSocket->peerAddress().toString() + + " 对等(peer)端口:" + QString::number(m_tcpSocket->peerPort())); + ui->actConnect->setEnabled(false); + ui->actDisconnect->setEnabled(true); +} + +void ExTcpClient::onDisconnected() +{ + ui->plainTextEdit->appendPlainText("已经断开与服务器的连接\n"); + ui->actConnect->setEnabled(true); + ui->actDisconnect->setEnabled(false); +} + +void ExTcpClient::onSocketReadyRead() +{ + while (m_tcpSocket->canReadLine()) { + ui->plainTextEdit->appendPlainText("[服务器:]" + m_tcpSocket->readLine()); + } +} + +void ExTcpClient::onSocketStateChange(QAbstractSocket::SocketState socketState) +{ + switch (socketState) { + case QAbstractSocket::UnconnectedState: + m_labSocket->setText("socket状态:UnconnectedState"); + break; + case QAbstractSocket::HostLookupState: + m_labSocket->setText("socket状态:HostLookupState"); + break; + case QAbstractSocket::ConnectingState: + m_labSocket->setText("socket状态:ConnectingState"); + break; + case QAbstractSocket::ConnectedState: + m_labSocket->setText("socket状态:ConnectedState"); + break; + case QAbstractSocket::BoundState: + m_labSocket->setText("socket状态:BoundState"); + break; + case QAbstractSocket::ClosingState: + m_labSocket->setText("socket状态:ClosingState"); + break; + case QAbstractSocket::ListeningState: + m_labSocket->setText("socket状态:ListeningState"); + break; + default: + m_labSocket->setText("socket状态:其他未知状态..."); + break; + } +} + void ExTcpClient::on_actConnect_triggered() { - + QString addr = ui->comboBox->currentText(); + quint16 port = ui->spinBox->value(); + m_tcpSocket->connectToHost(addr, port); } void ExTcpClient::on_actDisconnect_triggered() { - + if(m_tcpSocket->state() == QAbstractSocket::ConnectedState) + m_tcpSocket->disconnectFromHost(); } void ExTcpClient::on_actClear_triggered() @@ -49,3 +126,15 @@ void ExTcpClient::on_actQuit_triggered() { close(); } + +void ExTcpClient::on_btnSend_clicked() +{ + QString msg = ui->lineEdit->text(); + ui->plainTextEdit->appendPlainText("[客户端:]" + msg); + ui->lineEdit->clear(); + ui->lineEdit->setFocus(); + + QByteArray str = msg.toUtf8(); + str.append('\n'); + m_tcpSocket->write(str); +} diff --git a/QtTcpEx/ExTcpClient/ExTcpClient.h b/QtTcpEx/ExTcpClient/ExTcpClient.h index e242fb1..9f543e7 100644 --- a/QtTcpEx/ExTcpClient/ExTcpClient.h +++ b/QtTcpEx/ExTcpClient/ExTcpClient.h @@ -21,6 +21,8 @@ public: private: QString getLocalIp(); //获取本本机 IP +protected: + void closeEvent(QCloseEvent *event); private slots: //UI 定义的槽函数 @@ -28,6 +30,13 @@ private slots: void on_actDisconnect_triggered(); //断开与服务器的连接 void on_actClear_triggered(); //清除内容 void on_actQuit_triggered(); //退出程序 + void on_btnSend_clicked(); //发送文本消息 + + //自定义的槽函数 + void onConnected(); + void onDisconnected(); + void onSocketReadyRead(); //从socket读取传入的数据 + void onSocketStateChange(QAbstractSocket::SocketState socketState); private: Ui::ExTcpClient *ui; diff --git a/QtTcpEx/ExTcpClient/ExTcpClient.ui b/QtTcpEx/ExTcpClient/ExTcpClient.ui index 9fd8c82..932838a 100644 --- a/QtTcpEx/ExTcpClient/ExTcpClient.ui +++ b/QtTcpEx/ExTcpClient/ExTcpClient.ui @@ -113,6 +113,7 @@ + diff --git a/QtTcpEx/ExTcpServer/ExTcpServer.cpp b/QtTcpEx/ExTcpServer/ExTcpServer.cpp index f5f8fc6..111ca76 100644 --- a/QtTcpEx/ExTcpServer/ExTcpServer.cpp +++ b/QtTcpEx/ExTcpServer/ExTcpServer.cpp @@ -31,6 +31,7 @@ QString ExTcpServer::getLocalIp() { QString hostName = QHostInfo::localHostName(); QHostInfo hostInfo = QHostInfo::fromName(hostName); + ui->plainTextEdit->appendPlainText("本机名称:" + hostName); QString locaIp; QList list = hostInfo.addresses(); @@ -96,7 +97,7 @@ void ExTcpServer::on_actQuit_triggered() void ExTcpServer::on_btnSend_clicked() { QString msg = ui->lineEdit->text(); - ui->plainTextEdit->appendPlainText("[out]" + msg); + ui->plainTextEdit->appendPlainText("[服务器:]" + msg); ui->lineEdit->clear(); ui->plainTextEdit->hasFocus(); @@ -108,7 +109,7 @@ void ExTcpServer::on_btnSend_clicked() void ExTcpServer::onSocketReadyRead() //读取缓冲区行文本 { while (m_tcpSocket->canReadLine()) { - ui->plainTextEdit->appendPlainText("[in]" + m_tcpSocket->readLine()); + ui->plainTextEdit->appendPlainText("[客户端:]" + m_tcpSocket->readLine()); } } diff --git a/QtTcpEx/ExTcpServer/ExTcpServer.ui b/QtTcpEx/ExTcpServer/ExTcpServer.ui index 477308e..10026a2 100644 --- a/QtTcpEx/ExTcpServer/ExTcpServer.ui +++ b/QtTcpEx/ExTcpServer/ExTcpServer.ui @@ -6,8 +6,8 @@ 0 0 - 517 - 314 + 573 + 341 @@ -96,7 +96,7 @@ 0 0 - 517 + 573 25