• QT tcpserver


    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
    
        // 服务端有QTcpServer库,封装了监听操作
        server = new QTcpServer();
    
        // 直接监听,内部根据传入的ip和端口进行绑定
        server->listen(QHostAddress::AnyIPv4, SERVER_PORT);
    
        // 对server进行新的连接信号建立信号槽
        connect(server, &QTcpServer::newConnection, this, &Widget::newClientHandler);
    
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    
    void Widget::newClientHandler()
    {
        // 将获取到的新的连接套接字中获取客户端ip和端口
        socket = server->nextPendingConnection();
        ui->hostLineEdit->setText(socket->peerAddress().toString());
        ui->portLineEdit->setText(QString::number(socket->peerPort()));
    
    
        // 新的消息到来时,connect 数据read和处理信号槽函数
        connect(socket, &QTcpSocket::readyRead, this, &Widget::clientInfoSlot);
    }
    
    
    void Widget::clientInfoSlot()
    {
        ui->chatLineEdit->setText(QString(socket->readAll()));
    }
    
    void Widget::on_closeButton_clicked()
    {
        socket->close();
    }
    
    
    
    • 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
    // tcpclient
    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
        socket = new QTcpSocket;
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    
    void Widget::on_connectButton_clicked()
    {
        // 连接服务器
        socket->connectToHost(ui->hostLineEdit->text(), ui->portLineEdit->text().toInt());
    
        // 连接成功后,socket会发出一个connected的信号,使用lambda作为信号槽函数
        connect(socket, &QTcpSocket::connected, [this](){
            // 打开一个新的聊天窗口,这里不能局部变量,因为新窗口需要长久显示
            // 在堆区申请空间,否则对象生命周期结束后就释放了
            // 聊天窗口需要进行发送,所以需要socket
            // 原来的登录窗口需要进行隐藏
            this->hide();
            Chat *ct = new Chat(socket);
            ct->show();
        });
    }
    
    
    
    • 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
    // 建立连接成功后的聊天窗口
    #include "chat.h"
    #include "ui_chat.h"
    #
    Chat::Chat(QTcpSocket *socket, QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Chat)
    {
        ui->setupUi(this);
        this->socket = socket;
    }
    
    Chat::~Chat()
    {
        delete ui;
    }
    
    void Chat::on_sendButton_clicked()
    {
        QByteArray ba;
        ba.append(ui->chatLineEdit->text());
    
        socket->write(ba);
    }
    
    
    
    • 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
  • 相关阅读:
    python LeetCode 刷题记录 28
    Linux驱动开发(十四)---USB驱动开发学习(键盘+鼠标)
    嵌入式分享合集96
    Cell:水平基因转移在昆虫中广泛存在,增强鳞翅目雄性昆虫求偶行为
    探索腾讯企业邮箱替代方案:选择适合你的新邮件服务
    Linux系统运维脚本:shell脚本查看一定网段范围在线网络设备的ip地址和不在线的网络设备的数量(查看在线和不在线网络设备)
    Reactive UI -- 反应式编程UI框架入门学习(一)
    FPGA工程师面试试题集锦121~130
    A/B 测试:Python 分步指南
    《深入浅出Spring》父子容器
  • 原文地址:https://blog.csdn.net/CodeHouse/article/details/132759478