feat: TcpClient 客户端功能完成,且测试服务器和客户端之间的通讯成功

This commit is contained in:
touwoyimuli 2019-11-24 01:18:57 +08:00
parent 466761915f
commit 674ee71e87
5 changed files with 109 additions and 9 deletions

View File

@ -6,6 +6,21 @@ ExTcpClient::ExTcpClient(QWidget *parent) :
ui(new Ui::ExTcpClient) ui(new Ui::ExTcpClient)
{ {
ui->setupUi(this); 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() ExTcpClient::~ExTcpClient()
@ -21,23 +36,85 @@ QString ExTcpClient::getLocalIp()
QString localIp; QString localIp;
foreach (QHostAddress addr, hostInfo.addresses()) { foreach (QHostAddress addr, hostInfo.addresses()) {
if (QAbstractSocket::IPv4Protocol == addr.toString()) { if (QAbstractSocket::IPv4Protocol == addr.protocol()) {
localIp = addr.toString(); localIp = addr.toString();
break; break;
} }
} }
return localIp; 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() 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() void ExTcpClient::on_actDisconnect_triggered()
{ {
if(m_tcpSocket->state() == QAbstractSocket::ConnectedState)
m_tcpSocket->disconnectFromHost();
} }
void ExTcpClient::on_actClear_triggered() void ExTcpClient::on_actClear_triggered()
@ -49,3 +126,15 @@ void ExTcpClient::on_actQuit_triggered()
{ {
close(); 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);
}

View File

@ -21,6 +21,8 @@ public:
private: private:
QString getLocalIp(); //获取本本机 IP QString getLocalIp(); //获取本本机 IP
protected:
void closeEvent(QCloseEvent *event);
private slots: private slots:
//UI 定义的槽函数 //UI 定义的槽函数
@ -28,6 +30,13 @@ private slots:
void on_actDisconnect_triggered(); //断开与服务器的连接 void on_actDisconnect_triggered(); //断开与服务器的连接
void on_actClear_triggered(); //清除内容 void on_actClear_triggered(); //清除内容
void on_actQuit_triggered(); //退出程序 void on_actQuit_triggered(); //退出程序
void on_btnSend_clicked(); //发送文本消息
//自定义的槽函数
void onConnected();
void onDisconnected();
void onSocketReadyRead(); //从socket读取传入的数据
void onSocketStateChange(QAbstractSocket::SocketState socketState);
private: private:
Ui::ExTcpClient *ui; Ui::ExTcpClient *ui;

View File

@ -113,6 +113,7 @@
<addaction name="actClear"/> <addaction name="actClear"/>
<addaction name="actQuit"/> <addaction name="actQuit"/>
</widget> </widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actConnect"> <action name="actConnect">
<property name="icon"> <property name="icon">
<iconset resource="resource.qrc"> <iconset resource="resource.qrc">

View File

@ -31,6 +31,7 @@ QString ExTcpServer::getLocalIp()
{ {
QString hostName = QHostInfo::localHostName(); QString hostName = QHostInfo::localHostName();
QHostInfo hostInfo = QHostInfo::fromName(hostName); QHostInfo hostInfo = QHostInfo::fromName(hostName);
ui->plainTextEdit->appendPlainText("本机名称:" + hostName);
QString locaIp; QString locaIp;
QList<QHostAddress> list = hostInfo.addresses(); QList<QHostAddress> list = hostInfo.addresses();
@ -96,7 +97,7 @@ void ExTcpServer::on_actQuit_triggered()
void ExTcpServer::on_btnSend_clicked() void ExTcpServer::on_btnSend_clicked()
{ {
QString msg = ui->lineEdit->text(); QString msg = ui->lineEdit->text();
ui->plainTextEdit->appendPlainText("[out]" + msg); ui->plainTextEdit->appendPlainText("[服务器:]" + msg);
ui->lineEdit->clear(); ui->lineEdit->clear();
ui->plainTextEdit->hasFocus(); ui->plainTextEdit->hasFocus();
@ -108,7 +109,7 @@ void ExTcpServer::on_btnSend_clicked()
void ExTcpServer::onSocketReadyRead() //读取缓冲区行文本 void ExTcpServer::onSocketReadyRead() //读取缓冲区行文本
{ {
while (m_tcpSocket->canReadLine()) { while (m_tcpSocket->canReadLine()) {
ui->plainTextEdit->appendPlainText("[in]" + m_tcpSocket->readLine()); ui->plainTextEdit->appendPlainText("[客户端:]" + m_tcpSocket->readLine());
} }
} }

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>517</width> <width>573</width>
<height>314</height> <height>341</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -96,7 +96,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>517</width> <width>573</width>
<height>25</height> <height>25</height>
</rect> </rect>
</property> </property>