feat: TcpClient 客户端功能完成,且测试服务器和客户端之间的通讯成功
This commit is contained in:
parent
466761915f
commit
674ee71e87
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -113,6 +113,7 @@
|
||||
<addaction name="actClear"/>
|
||||
<addaction name="actQuit"/>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusBar"/>
|
||||
<action name="actConnect">
|
||||
<property name="icon">
|
||||
<iconset resource="resource.qrc">
|
||||
|
@ -31,6 +31,7 @@ QString ExTcpServer::getLocalIp()
|
||||
{
|
||||
QString hostName = QHostInfo::localHostName();
|
||||
QHostInfo hostInfo = QHostInfo::fromName(hostName);
|
||||
ui->plainTextEdit->appendPlainText("本机名称:" + hostName);
|
||||
QString locaIp;
|
||||
|
||||
QList<QHostAddress> 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());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,8 +6,8 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>517</width>
|
||||
<height>314</height>
|
||||
<width>573</width>
|
||||
<height>341</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
@ -96,7 +96,7 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>517</width>
|
||||
<width>573</width>
|
||||
<height>25</height>
|
||||
</rect>
|
||||
</property>
|
||||
|
Loading…
Reference in New Issue
Block a user