• QTcpServer简单的TCP服务器连接


    1、简介

    简单实现控制TCP服务器获取连接的套接字。点击断开服务器即可关闭所有连接,最大连接数量为5个。

    声明源文件

    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        //设置固定大小
        setFixedSize(1024,600);
    
        btnClearText = new QPushButton(this);
        btnCtrlServer = new QPushButton(this);
        textRecvBrowser = new QTextBrowser(this);
    
        //按键清空
        btnClearText->setGeometry(48,40,200,60);
        btnClearText->setText("清空浏览");
    
        //设置控制按钮
        btnCtrlServer->setGeometry(48, 40+40+30 , 200, 60);
        btnCtrlServer->setText("打开服务");
    
        //浏览处理
        textRecvBrowser->setGeometry(300, 40, 649, 500);
    
        //连接处理
        QObject::connect(btnClearText, SIGNAL(clicked()), this, SLOT(on_pushButton_textRecvBrowser()));
        QObject::connect(btnCtrlServer, SIGNAL(clicked()),this, SLOT(on_pushButton_ctrlTcpServer()));
    
        //TCP服务器处理
        tcpServer = new QTcpServer(this);
        QObject::connect(tcpServer, SIGNAL(newConnection()),this, SLOT(on_tcpServer_newConnectCallHandler()));
    
        //最大连接数量5个
        tcpServer->setMaxPendingConnections(5);
        tcpServer->listen(QHostAddress("127.0.0.1"), 80);
        if(tcpServer->isListening())
            btnCtrlServer->setText("关闭服务");
        else
            btnCtrlServer->setText("打开服务");
    
    }
    
    MainWindow::~MainWindow()
    {
        delete btnClearText;
        delete btnCtrlServer;
        delete textRecvBrowser;
        delete tcpServer;
    }
    
    //清空浏览记录
    void MainWindow::on_pushButton_textRecvBrowser()
    {
        textRecvBrowser->clear();
    }
    
    //
    void MainWindow::on_pushButton_ctrlTcpServer()
    {
        //状态查询
        if(tcpServer->isListening())
        {
            //遍历所有数据
            foreach(QTcpSocket* tcpSocket, tcpSocketList)
            {
                if(tcpSocket->state() == QAbstractSocket::ConnectedState)
                    tcpSocket->close();//关闭连接
            }
    
            //
            tcpServer->close();
        }
        else
            tcpServer->listen(QHostAddress::Any, 80);
    
        //监听状态
        if(tcpServer->isListening())
            btnCtrlServer->setText("关闭服务");
        else
            btnCtrlServer->setText("打开服务");
    }
    
    //IPv6转换IPv4
    QString convert_to_ipv4_addr(QHostAddress &addr)
    {
        quint32  addr_origin = addr.toIPv4Address();
        QHostAddress addr_host = QHostAddress(addr_origin);
        QString  addr_str = addr_host.toString();
        return addr_str;
    }
    
    //新建连接
    void MainWindow::on_tcpServer_newConnectCallHandler()
    {
        while (tcpServer->hasPendingConnections()) {
    
            //获取连接
            QTcpSocket *tcpSocket = tcpServer->nextPendingConnection();
    
            //添加指针列表
            tcpSocketList.append(tcpSocket);
    
            //显示具体连接信息
            QHostAddress ipv6 = tcpSocket->peerAddress();
            textRecvBrowser->append(convert_to_ipv4_addr(ipv6) + ":" + QString::number(tcpSocket->peerPort()));
    
            //设置连接
            QObject::connect(tcpSocket, SIGNAL(readyRead()),this, SLOT(on_tcpSocket_readyRead()));
            QObject::connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(on_tcpSocket_disconnected()));
    
            //
            qDebug() << "当前连接数量" << tcpSocketList.size();
        }
    
    }
    
    void MainWindow::on_tcpSocket_disconnected()
    {
        QTcpSocket* socket = qobject_cast<QTcpSocket *>(sender()); //当前信息
    
        //断开所有连接
        if(socket->state() == QAbstractSocket::ConnectedState)
            socket->close();//关闭连接
    
        //断开连接则清除
        tcpSocketList.removeOne(socket);
    
        textRecvBrowser->append("已断开连接");
    
        qDebug() << "当前连接数量" << tcpSocketList.size();
    }
    
    //
    void MainWindow::on_tcpSocket_readyRead()
    {
        QTcpSocket* socket = qobject_cast<QTcpSocket *>(sender());  // 取得当前socket对象
    
        socket->write("Hello",5);
    //    QMessageBox msgBox(this);
    
    //    msgBox.setText("我是准备好读数据");
    //    msgBox.setWindowTitle("提示");
    //    msgBox.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144

    声明的头文件

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void on_pushButton_textRecvBrowser();
        void on_pushButton_ctrlTcpServer();
        void on_tcpServer_newConnectCallHandler();
    
    
        void on_tcpSocket_readyRead();
        void on_tcpSocket_disconnected();
    private:
        QList<QTcpSocket *>  tcpSocketList;
    
        QPushButton   *btnCtrlServer = nullptr;
        QPushButton   *btnClearText = nullptr;
        QTextBrowser  *textRecvBrowser = nullptr;
        QTcpServer    *tcpServer = nullptr;
    };
    #endif // MAINWINDOW_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    Theme Studio(主题工作室)
    基于STM32_DS18B20单总线传感器驱动
    低轨互联网产业发展探究
    rancher-rke2 修改--service-cluster-ip-range
    决策树——预剪枝和后剪枝
    6.25AtCoderABC257E - Addition and Multiplication 2题解
    VirtualBox中安装MacOS Big Sur
    重新定义分析 - EventBridge 实时事件分析平台发布
    ThreeJS - 封装一个GLB模型展示组件(TypeScript)
    物理层 (physical layer)
  • 原文地址:https://blog.csdn.net/qq_36883460/article/details/134099947