• QT自制TCP服务器


    TCP服务器

    TCP服务器实现对TCP客户端的监听,实现对数据的发送与接收。
    在这里插入图片描述
    该TCP服务器入门级别,后面可以继续丰富。

    界面

    上图就是实现的界面,后面可以自己去丰富一下,上面的按钮,接收数据框,发送数据框,端口号输入框,文字显示等使用的控件为:

    1. 文字显示(除去按钮,按钮可以直接写)
      在这里插入图片描述

    2. 端口号输入框
      和后面发送框一样的控件
      在这里插入图片描述

    3. 按钮
      在这里插入图片描述

    4. 发送框
      在这里插入图片描述

    5. 接收框

    在这里插入图片描述
    注意:此控件设置为只读模式,因为不需要我们输入。

    逻辑代码

    .pro文件

    QT       += core gui network//这个地方需要加上这个就可以了
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    CONFIG += c++11
    
    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
        main.cpp \
        widget.cpp
    
    HEADERS += \
        widget.h
    
    FORMS += \
        widget.ui
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    
    • 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

    .h文件

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QHostAddress>
    #include <QtNetwork>//需要的头文件
    QT_BEGIN_NAMESPACE
    namespace Ui { class Widget; }
    QT_END_NAMESPACE
    class Widget : public QWidget
    {
        Q_OBJECT
    public:
        Widget(QWidget *parent = nullptr);
        ~Widget();
        QTcpServer *tcpServer;
        QTcpSocket *tcpSocket;
    private slots:
        void newConnection_Slot();
        void readyRead_Slot();
        void disconnected_Slot();
        void on_openbt_clicked();
        void on_closeBt_clicked();
        void on_sendBT_clicked();
    private:
        Ui::Widget *ui;
    };
    #endif // WIDGET_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

    此处添加头文件,声明槽函数。

    .cpp文件

    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
        tcpServer = new QTcpServer(this);
        tcpSocket = new QTcpSocket(this);
    //    tcpServer->listen(QHostAddress::LocalHost, ui->duankouEdit_2->text().toUInt());
    //    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection_Slot()));
    }
    void Widget::newConnection_Slot()
    {
        static int i = 0;
        tcpSocket = tcpServer->nextPendingConnection();//创建socket连接
        connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readyRead_Slot()));//创建读数据凹槽
        connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected_Slot()));//创建服务端失联凹槽
        i++;
    }
    void Widget::readyRead_Slot()
    {
        QString buf;
        buf = tcpSocket->readAll();
        ui->receiveEdit->appendPlainText(buf);
    }
    void Widget::disconnected_Slot()
    {
    }
    Widget::~Widget()
    {
        delete ui;
    }
    void Widget::on_openbt_clicked()
    {
        tcpServer->listen(QHostAddress::Any, ui->duankouEdit_2->text().toUInt());
        connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection_Slot()));
        QString localhostName = QHostInfo::localHostName();
        QHostInfo info = QHostInfo::fromName(localhostName);
        ui->receiveEdit->appendPlainText(localhostName);
        ui->receiveEdit->appendPlainText(info.addresses().back().toString());//first:ipv6地址,back:ipv4地址
        foreach(QHostAddress address,info.addresses())//寻找ipv4地址
        {
             if(address.protocol() == QAbstractSocket::IPv4Protocol)
                ui->receiveEdit->appendPlainText(address.toString());
        }
        quint16 port = tcpServer->serverPort();
        QString st = QString::number(port);
        ui->receiveEdit->appendPlainText(st);
    }
    void Widget::on_closeBt_clicked()
    {
        tcpServer->close();
        tcpSocket->close();
        disconnect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection_Slot()));
    }
    void Widget::on_sendBT_clicked()
    {
         tcpSocket->write(ui->sendEdit->text().toLocal8Bit().data());
    }
    
    • 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

    打开服务器获取主机IP地址的两种方法:

    	QHostInfo info = QHostInfo::fromName(localhostName);
        ui->receiveEdit->appendPlainText(localhostName);
        ui->receiveEdit->appendPlainText(info.addresses().back().toString());
        //first:ipv6地址,back:ipv4地址
        foreach(QHostAddress address,info.addresses())//寻找ipv4地址
        {
             if(address.protocol() == QAbstractSocket::IPv4Protocol)
                ui->receiveEdit->appendPlainText(address.toString());
        }
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    项目展示

    tcp服务器:
    在这里插入图片描述
    tcp客户端:在这里插入图片描述
    打开服务器的时候,服务器端会自动输出当前服务器的IP地址和端口号。
    然后将IP地址和端口号输入到客户端里面,打开客户端连接就可以了。
    实现了服务器与客户端的通信。

    源码资源下载

    https://download.csdn.net/download/qq_30255657/85675636

  • 相关阅读:
    云原生数据库的到来
    Kafka-SSL笔记整理
    C++转换函数
    CKKS同态加密方案初步学习
    11.18MyBatis 学习2
    C++--第一个代码hello world
    Ant-design中表单多级对象做嵌套表单校验
    如何把A3 pdf 文章打印成A4
    蓝蜂物联网水肥一体化MQTT应用案例
    用户促活留存新方式——在APP中嵌入小游戏
  • 原文地址:https://blog.csdn.net/qq_30255657/article/details/125337388