• qt tcp 连接 秒断连


    问题:
    tcp连接总是秒成功后断连

    debug会出现下面这些

    onecore\net\netprofiles\service\src\nsp\dll\namespaceserviceprovider.cpp(550)\nlansp_c.dll!00007FFDA2A1D93D: (caller: 00007FFDD8BEACF6) LogHr(1) tid(336c) 8007277C ´Ë·þÎñ²»´æÔÚ¡£ÔÚÖ¸¶¨µÄÃüÃû¿Õ¼äÖÐÕÒ²»Õâ¸ö·þÎñ¡£

    onecore\net\netprofiles\service\src\nsp\dll\namespaceserviceprovider.cpp(1265)\nlansp_c.dll!00007FFDA2A1CD90: (caller: 00007FFDD8BF15A8) LogHr(2) tid(336c) FFFFFFFF

    我重装了qt,装的时候勾上MingG 32的编译器,用起来就没事了,tcp可以稳定连接,不过还是会报上面的warning

    窗口源码:

    #include "login.h"
    #include "ui_login.h"
    
    Login::Login(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Login)
    {
        ui->setupUi(this);
        sock = new QTcpSocket(this);//Login的成员变量QTcpSocket*
        sock->abort();
    
        //与服务器连接成功会发出信号
        connect(sock,&QTcpSocket::connected,[this]()
        {
            chatwindow = new ChatWindow(sock,this);
            chatwindow->set_user_name(ui->usernameTextEdit->text());
            chatwindow->show();
            this->hide();
            QMessageBox::information(this,"连接提示","连接成功!");
    
        });
    
        connect(sock,&QTcpSocket::disconnected,[this](){
            QMessageBox::warning(this,"连接提示","异常!连接断开!");
        });
    }
    
    Login::~Login()
    {
        delete ui;
    }
    
    //连接按钮
    void Login::on_pushButton_clicked()
    {
        QString name_string = ui->usernameTextEdit->text();
        if(name_string.count() ==0)
        {
            QMessageBox::information(this,"输入提示","请输入用户名!");
            return;
        }
    
        QString ip_string = ui->ipTextEdit->text();
        QString port_string = ui->portTextEdit->text();
    
    
        qDebug() << ip_string;
        qDebug() << port_string;
        sock->abort();
        sock->connectToHost(QHostAddress(ip_string),port_string.toInt());
    }
    
    • 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

    debug内容
    在这里插入图片描述

    //Chatwindow的源码

    #include "chatwindow.h"
    #include "ui_chatwindow.h"
    
    ChatWindow::ChatWindow(QTcpSocket* sock_ref,QWidget *parent) :
        QWidget(parent),
        ui(new Ui::ChatWindow)
    {
        ui->setupUi(this);
        this->sock = sock_ref;//ChatWindow的成员变量QTcpSocket*
        qDebug()<< "ChatWindow start";
    }
    
    ChatWindow::~ChatWindow()
    {
        delete ui;
        qDebug()<< "ChatWindow end";
    }
    
    
    
    void ChatWindow::set_user_name(QString user_name)
    {
        msg["user_name"] = user_name;
        qDebug()<<"ChatWindow set user name :" << msg["user_name"];
    }
    
    void ChatWindow::on_sendPushButton_clicked()
    {
    
    }
    
    
    
    • 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
  • 相关阅读:
    01-基于imx6ul从0自制Bootloader专栏实现总结
    Java 世界的法外狂徒:反射
    Linux中文件查找相关命令比较
    网站被DDOS攻击怎么办?防护经验!
    23 张图细讲使用 Devtron 简化 K8S 中应用开发
    12.2-12.4总结
    如何衡量软件系统的复杂度(三)
    虹科分享|终端安全防护|网络安全术语列表(二)
    jar包导入到本地仓库无法引用
    Java毕业设计源代码评教评分教务管理系统springboot
  • 原文地址:https://blog.csdn.net/m0_72193120/article/details/137869547