当前位置:   article > 正文

【QT篇】网络调试助手_qt网络调试助手

qt网络调试助手

一、项目概述

1.功能介绍

•支持开启服务端监听,客户端连接

•支持客户端、服务端收发消息

•支持多客户端连接服务端

2.界面预览

二、常用类 

在此主要罗列一些主要使用的类,具体请查阅QT帮助手册

•QTcpServer
•QTcpSocket
•QNetworkInterface
•QHostAddress
•QMouseEvent
•QTextCharFormat

三、项目源码

https://github.com/GeekerGao/TCP-Client-Server

Github登陆不上,可私信要网盘链接!

部分代码

server

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QMessageBox>
  4. #include <QNetworkInterface>
  5. #include <QTcpSocket>
  6. Widget::Widget(QWidget *parent)
  7. : QWidget(parent)
  8. , ui(new Ui::Widget)
  9. {
  10. ui->setupUi(this);
  11. this->setLayout(ui->verticalLayout);
  12. server = new QTcpServer(this);
  13. connect(ui->comboBoxChildren,&MyComboBox::on_ComboBox_clicked,this,&Widget::mComboBox_refresh);
  14. connect(server,SIGNAL(newConnection()),this,SLOT(on_newClient_connect()));
  15. ui->btnApart->setEnabled(false);
  16. ui->btnStopListen->setEnabled(false);
  17. ui->btnSend->setEnabled(false);
  18. QList<QHostAddress> address = QNetworkInterface::allAddresses();
  19. for(QHostAddress tmp : address){
  20. if(tmp.protocol() == QAbstractSocket::IPv4Protocol){
  21. ui->comboBoxAddr->addItem(tmp.toString());
  22. }
  23. }
  24. }
  25. Widget::~Widget()
  26. {
  27. delete ui;
  28. }
  29. void Widget::on_newClient_connect()
  30. {
  31. if(server->hasPendingConnections()){
  32. QTcpSocket* connection = server->nextPendingConnection();
  33. ui->textEditRev->insertPlainText("客户端地址:"+connection->peerAddress().toString()
  34. +"\n客户端端口号:"+QString::number(connection->peerPort())+"\n");
  35. connect(connection,SIGNAL(readyRead()),this,SLOT(on_readyRead_handler()));
  36. connect(connection,SIGNAL(stateChanged(QAbstractSocket::SocketState)),
  37. this,SLOT(mstateChanged(QAbstractSocket::SocketState)));
  38. ui->comboBoxChildren->addItem(QString::number(connection->peerPort()));
  39. ui->comboBoxChildren->setCurrentText(QString::number(connection->peerPort()));
  40. if(!ui->btnSend->isEnabled()){
  41. ui->btnSend->setEnabled(true);
  42. }
  43. }
  44. }
  45. void Widget::on_btnListen_clicked()
  46. {
  47. int port = ui->lineEditPort->text().toInt();
  48. if(!server->listen(QHostAddress(ui->comboBoxAddr->currentText()),port)){
  49. QMessageBox msgBox;
  50. msgBox.setWindowTitle("监听失败");
  51. msgBox.setText("端口号被占用");
  52. msgBox.exec();
  53. return;
  54. }
  55. ui->btnListen->setEnabled(false);
  56. ui->btnApart->setEnabled(true);
  57. ui->btnStopListen->setEnabled(true);
  58. }
  59. void Widget::on_readyRead_handler()
  60. {
  61. QTcpSocket *tmpSock = qobject_cast<QTcpSocket *>(sender());
  62. QByteArray revData = tmpSock->readAll();
  63. ui->textEditRev->moveCursor(QTextCursor::End);
  64. ui->textEditRev->ensureCursorVisible();
  65. ui->textEditRev->insertPlainText("客户端:"+revData+"\n");
  66. }
  67. void Widget::mdisconnected()
  68. {
  69. QTcpSocket *tmpSock = qobject_cast<QTcpSocket *>(sender());
  70. ui->textEditRev->insertPlainText("客户端断开!\n");
  71. tmpSock->deleteLater();
  72. }
  73. void Widget::mstateChanged(QAbstractSocket::SocketState socketState)
  74. {
  75. int tmpIndex;
  76. QTcpSocket *tmpSock = qobject_cast<QTcpSocket *>(sender());
  77. switch(socketState){
  78. case QAbstractSocket::UnconnectedState:
  79. ui->textEditRev->insertPlainText("客户端断开!\n");
  80. tmpIndex = ui->comboBoxChildren->findText(QString::number(tmpSock->peerPort()));
  81. ui->comboBoxChildren->removeItem(tmpIndex);
  82. tmpSock->deleteLater();
  83. if(ui->comboBoxChildren->count() == 0)
  84. ui->btnSend->setEnabled(false);
  85. break;
  86. case QAbstractSocket::ConnectedState:
  87. case QAbstractSocket::ConnectingState:
  88. ui->textEditRev->insertPlainText("客户端接入!");
  89. break;
  90. }
  91. }
  92. void Widget::mComboBox_refresh()
  93. {
  94. ui->comboBoxChildren->clear();
  95. QList<QTcpSocket*> tcpSocketClients = server->findChildren<QTcpSocket*>();
  96. for(QTcpSocket* tmp : tcpSocketClients){
  97. if(tmp != nullptr)
  98. ui->comboBoxChildren->addItem(QString::number(tmp->peerPort()));
  99. }
  100. ui->comboBoxChildren->addItem("All");
  101. }
  102. void Widget::on_btnSend_clicked()
  103. {
  104. QList<QTcpSocket*> tcpSocketClients = server->findChildren<QTcpSocket*>();
  105. //当用户不选择向所有客户端进行发送时候
  106. if(tcpSocketClients.isEmpty()){
  107. QMessageBox msgBox;
  108. msgBox.setWindowTitle("发送错误!");
  109. msgBox.setText("当前无连接!");
  110. msgBox.exec();
  111. ui->btnSend->setEnabled(false);
  112. return;
  113. }
  114. if(ui->comboBoxChildren->currentText() != "All"){
  115. QString currentName = ui->comboBoxChildren->currentText();
  116. for(QTcpSocket* tmp : tcpSocketClients){
  117. if(QString::number(tmp->peerPort()) == currentName){
  118. tmp->write(ui->textEditSend->toPlainText().toStdString().c_str());
  119. }
  120. }
  121. }else{
  122. //遍历所有子客户端,并一一调用write函数,向所有客户端发送消息
  123. for(QTcpSocket* tmp : tcpSocketClients){
  124. QByteArray sendData = ui->textEditSend->toPlainText().toUtf8();
  125. tmp->write(sendData);
  126. }
  127. }
  128. }
  129. void Widget::on_btnStopListen_clicked()
  130. {
  131. QList<QTcpSocket*> tcpSocketClients = server->findChildren<QTcpSocket*>();
  132. for(QTcpSocket* tmp : tcpSocketClients){
  133. tmp->close();
  134. }
  135. server->close();
  136. ui->btnListen->setEnabled(true);
  137. ui->btnApart->setEnabled(false);
  138. ui->btnStopListen->setEnabled(false);
  139. }
  140. void Widget::on_btnApart_clicked()
  141. {
  142. on_btnStopListen_clicked();
  143. delete server;
  144. this->close();
  145. }

client

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QTimer>
  4. Widget::Widget(QWidget *parent)
  5. : QWidget(parent)
  6. , ui(new Ui::Widget)
  7. {
  8. ui->setupUi(this);
  9. this->setLayout(ui->verticalLayout);
  10. ui->btnDisconnected->setEnabled(false);
  11. ui->btnSend->setEnabled(false);
  12. client = new QTcpSocket(this);
  13. connect(client,SIGNAL(readyRead()),this,SLOT(mRead_data_From_Server()));
  14. }
  15. Widget::~Widget()
  16. {
  17. delete ui;
  18. }
  19. void Widget::on_btnConnected_clicked()
  20. {
  21. client->connectToHost(ui->lineEditIPAddr->text(),ui->lineEditPort->text().toInt());
  22. timer = new QTimer(this);
  23. timer->setSingleShot(true);
  24. timer->setInterval(5000);
  25. connect(timer,SIGNAL(timeout()),this,SLOT(onTimeout()));
  26. connect(client,SIGNAL(connected()),this,SLOT(onConnected()));
  27. connect(client,SIGNAL(error(QAbstractSocket::SocketError)),
  28. this,SLOT(onError(QAbstractSocket::SocketError)));
  29. this->setEnabled(false);
  30. timer->start();
  31. }
  32. void Widget::mRead_data_From_Server()
  33. {
  34. ui->textEditRev->moveCursor(QTextCursor::End);
  35. ui->textEditRev->ensureCursorVisible();
  36. mInsertTextByColor(Qt::black,client->readAll());
  37. }
  38. void Widget::on_btnSend_clicked()
  39. {
  40. QByteArray sendData = ui->textEditSend->toPlainText().toUtf8();
  41. client->write(sendData);
  42. mInsertTextByColor(Qt::red,sendData);
  43. }
  44. void Widget::on_btnDisconnected_clicked()
  45. {
  46. client->disconnectFromHost();
  47. client->close();
  48. ui->textEditRev->append("中止连接!");
  49. ui->btnConnected->setEnabled(true);
  50. ui->lineEditPort->setEnabled(true);
  51. ui->lineEditIPAddr->setEnabled(true);
  52. ui->btnDisconnected->setEnabled(false);
  53. ui->btnSend->setEnabled(false);
  54. }
  55. void Widget::onConnected()
  56. {
  57. timer->stop();
  58. this->setEnabled(true);
  59. ui->textEditRev->append("连接成功!");
  60. ui->btnConnected->setEnabled(false);
  61. ui->lineEditPort->setEnabled(false);
  62. ui->lineEditIPAddr->setEnabled(false);
  63. ui->btnDisconnected->setEnabled(true);
  64. ui->btnSend->setEnabled(true);
  65. }
  66. void Widget::onError(QAbstractSocket::SocketError error)
  67. {
  68. ui->textEditRev->insertPlainText("连接出问题:"+client->errorString());
  69. this->setEnabled(true);
  70. on_btnDisconnected_clicked();
  71. }
  72. void Widget::onTimeout()
  73. {
  74. ui->textEditRev->insertPlainText("连接超时!");
  75. client->abort();
  76. this->setEnabled(true);
  77. }
  78. void Widget::mInsertTextByColor(Qt::GlobalColor color,QString str)
  79. {
  80. QTextCursor cursor = ui->textEditRev->textCursor();
  81. QTextCharFormat format;
  82. format.setForeground(QBrush(QColor(color)));
  83. cursor.setCharFormat(format);
  84. cursor.insertText(str);
  85. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/583675
推荐阅读
相关标签
  

闽ICP备14008679号