• QT实现TCP服务器客户端搭建的代码,现象


    1.效果

    2.服务器:

       2.1:ui界面

    2.2:头文件

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. QT_BEGIN_NAMESPACE
    10. namespace Ui { class Widget; }
    11. QT_END_NAMESPACE
    12. class Widget : public QWidget
    13. {
    14. Q_OBJECT
    15. public:
    16. Widget(QWidget *parent = nullptr);
    17. ~Widget();
    18. private slots:
    19. void on_pushButton_clicked();
    20. void newConnection_slot();
    21. void readyRead_slot();
    22. private:
    23. Ui::Widget *ui;
    24. QTcpServer *server;
    25. QList clientList;
    26. };
    27. #endif // WIDGET_H

    2.3:cpp文件

    main:

    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

    函数封装

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. , ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. server=new QTcpServer(this);
    9. connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
    10. }
    11. Widget::~Widget()
    12. {
    13. delete ui;
    14. }
    15. void Widget::on_pushButton_clicked()
    16. {
    17. if(ui->pushButton->text()=="启动")
    18. {
    19. ui->pushButton->setText("关闭");
    20. quint16 port=ui->lineEdit->text().toUInt();
    21. if(!server->listen(QHostAddress::Any,port))
    22. {
    23. QMessageBox::information(this,"失败","监听失败");
    24. }else
    25. {
    26. QMessageBox::information(this,"成功","服务器启动成功");
    27. }
    28. }else{
    29. for(int i=0;isize();i++)
    30. {
    31. clientList.removeAt(i);
    32. }
    33. server->close();
    34. ui->pushButton->setText("启动");
    35. }
    36. }
    37. void Widget::newConnection_slot()
    38. {
    39. qDebug()<<"您有新的客户端发来了连接请求了";
    40. QTcpSocket *s=server->nextPendingConnection();
    41. clientList.push_back(s);
    42. connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
    43. }
    44. void Widget::readyRead_slot()
    45. {
    46. qDebug()<<"有新的客户端信息发来了";
    47. for(int i=0;isize();i++)
    48. {
    49. if(clientList[i]->state()==0)
    50. {
    51. clientList.removeAt(i);
    52. }
    53. }
    54. for(int i=0;isize();i++)
    55. {
    56. if(clientList[i]->bytesAvailable()!=0)
    57. {
    58. QByteArray msg=clientList[i]->readAll();
    59. ui->msgWidget->addItem(QString::fromLocal8Bit(msg));
    60. for(int j=0;jsize();j++)
    61. {
    62. clientList[j]->write(msg);
    63. }
    64. }
    65. }
    66. }

    2.4:pro文件

    1. QT += core gui network
    2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    3. CONFIG += c++11
    4. # The following define makes your compiler emit warnings if you use
    5. # any Qt feature that has been marked deprecated (the exact warnings
    6. # depend on your compiler). Please consult the documentation of the
    7. # deprecated API in order to know how to port your code away from it.
    8. DEFINES += QT_DEPRECATED_WARNINGS
    9. # You can also make your code fail to compile if it uses deprecated APIs.
    10. # In order to do so, uncomment the following line.
    11. # You can also select to disable deprecated APIs only up to a certain version of Qt.
    12. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    13. SOURCES += \
    14. main.cpp \
    15. widget.cpp
    16. HEADERS += \
    17. widget.h
    18. FORMS += \
    19. widget.ui
    20. # Default rules for deployment.
    21. qnx: target.path = /tmp/$${TARGET}/bin
    22. else: unix:!android: target.path = /opt/$${TARGET}/bin
    23. !isEmpty(target.path): INSTALLS += target

    3.客户端:

       3.1:ui界面

    3.2:头文件

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. QT_BEGIN_NAMESPACE
    7. namespace Ui { class Widget; }
    8. QT_END_NAMESPACE
    9. class Widget : public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. Widget(QWidget *parent = nullptr);
    14. ~Widget();
    15. private slots:
    16. void on_pushButton_2_clicked();
    17. void connect_slot();
    18. void readyRead_slot();
    19. void on_pushButton_1_clicked();
    20. void on_pushButton_3_clicked();
    21. void disconnected_slot();
    22. private:
    23. Ui::Widget *ui;
    24. QTcpSocket *socket;
    25. QString userName;
    26. };
    27. #endif // WIDGET_H

    3.3:cpp文件

    main:

    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

    函数封装:

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. , ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. socket =new QTcpSocket(this);
    9. connect(socket,&QTcpSocket::connected,this,&Widget::connect_slot);
    10. connect(socket,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
    11. connect(socket,&QTcpSocket::disconnected,this,&Widget::disconnected_slot);
    12. }
    13. Widget::~Widget()
    14. {
    15. delete ui;
    16. }
    17. void Widget::on_pushButton_2_clicked()
    18. {
    19. userName=ui->lineEdit_2->text();
    20. QString ip=ui->lineEdit_3->text();
    21. quint16 port=ui->lineEdit_4->text().toUInt();
    22. socket->connectToHost(ip,port);
    23. }
    24. void Widget::connect_slot()
    25. {
    26. QMessageBox::information(this,"成功","您已经成功进入聊天室");
    27. QString msg=userName +":进入聊天室";
    28. socket->write(msg.toLocal8Bit());
    29. }
    30. void Widget::readyRead_slot()
    31. {
    32. QByteArray msg=socket->readAll();
    33. ui->listWidget->addItem(QString::fromLocal8Bit(msg));
    34. }
    35. void Widget::on_pushButton_1_clicked()
    36. {
    37. QString msg =userName+":"+ui->lineEdit->text();
    38. socket->write(msg.toLocal8Bit());
    39. ui->lineEdit->clear();
    40. }
    41. void Widget::on_pushButton_3_clicked()
    42. {
    43. QString msg=userName+":离开聊天室";
    44. socket->write(msg.toLocal8Bit());
    45. socket->disconnectFromHost();
    46. }
    47. void Widget::disconnected_slot()
    48. {
    49. QMessageBox::information(this,"提示","退出成功");
    50. }

    3.4:pro文件

    1. QT += core gui network
    2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    3. CONFIG += c++11
    4. # The following define makes your compiler emit warnings if you use
    5. # any Qt feature that has been marked deprecated (the exact warnings
    6. # depend on your compiler). Please consult the documentation of the
    7. # deprecated API in order to know how to port your code away from it.
    8. DEFINES += QT_DEPRECATED_WARNINGS
    9. # You can also make your code fail to compile if it uses deprecated APIs.
    10. # In order to do so, uncomment the following line.
    11. # You can also select to disable deprecated APIs only up to a certain version of Qt.
    12. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    13. SOURCES += \
    14. main.cpp \
    15. widget.cpp
    16. HEADERS += \
    17. widget.h
    18. FORMS += \
    19. widget.ui
    20. # Default rules for deployment.
    21. qnx: target.path = /tmp/$${TARGET}/bin
    22. else: unix:!android: target.path = /opt/$${TARGET}/bin
    23. !isEmpty(target.path): INSTALLS += target

  • 相关阅读:
    雅思成绩单上的这个符号, CEFR 究竟是什么意思
    火山引擎 ByteHouse:如何提升 18000 节点的 ClickHouse 可用性?
    【数据库】数据库的一级二级三级封锁协议
    java:过滤器Filter
    MySQL数据库——约束
    [Linux]----文件操作(复习C语言+文件描述符)
    ZAB协议
    vue笔记(三)
    Java基础之浅谈异常与了解断言
    上海青浦区大众驾校(科目二·自动挡)真实考场操作全程
  • 原文地址:https://blog.csdn.net/yjh666jhy/article/details/133518585