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

窗口源码:

cpp 复制代码
#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());
}

debug内容

//Chatwindow的源码

cpp 复制代码
#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()
{

}
相关推荐
TMT星球2 分钟前
Crybaby特展北京站开幕 泡泡玛特以IP承载情绪表达
tcp/ip
清水迎朝阳1 小时前
客户端软件 — 用户统计方案
服务器·c++·客户端·用户统计
再卷也是菜2 小时前
C++17(下)
c++
alexwang2113 小时前
HDU 4348 详细题解
c++·算法·题解·hdu·主席树·可持久化数据结构·可持久化线段树
程序员zgh3 小时前
C++ 拷贝赋值运算符 详解
c语言·开发语言·c++
皓月斯语4 小时前
B2132 素数对
c++·算法·题解
星轨初途5 小时前
LeetCode 热题 100——day2 字母异位词分组
c++·算法·leetcode
笨鸟先飞的橘猫6 小时前
c++面向对象编程
开发语言·c++
啦啦啦啦啦zzzz6 小时前
设计模式:桥接模式和组合模式
c++·设计模式·组合模式·桥接模式
-森屿安年-6 小时前
647. 回文子串
c++·算法·动态规划