QtExamples/QtTcpEx/ExTcpClient/ExTcpClient.cpp
touwoyimuli 098d039040 feat: QTcpServer and QTcpSocket
TCP通信之QTcpServer和QTcpSocket,服务器和客户端通讯
2019-11-25 20:32:39 +08:00

162 lines
4.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
*
* Author: touwoyimuli <touwoyimuli@gmai.com>
*
* github: https://github.com/touwoyimuli
* blogs: https://touwoyimuli.github.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://touwoyimuli.github.io/>.
*/
#include "ExTcpClient.h"
#include "ui_ExTcpClient.h"
ExTcpClient::ExTcpClient(QWidget *parent) :
QMainWindow(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()
{
delete ui;
}
QString ExTcpClient::getLocalIp()
{
QString hostName = QHostInfo::localHostName();
QHostInfo hostInfo = QHostInfo::fromName(hostName);
ui->plainTextEdit->appendPlainText("本机名称:" + hostName);
QString localIp;
foreach (QHostAddress addr, hostInfo.addresses()) {
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()
{
ui->plainTextEdit->clear();
}
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);
}