首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当调用QTcpServer时,程序崩溃

当调用QTcpServer时,程序崩溃
EN

Stack Overflow用户
提问于 2015-12-14 15:38:46
回答 1查看 97关注 0票数 1

我正在尝试一个非常非常简单的QT网络程序。由于某种原因,它在执行时崩溃,没有任何错误消息,因为它没有像预期的那样输出到命令行的任何输出。下面是代码:

qtTCPservertest.pro

代码语言:javascript
复制
QT       += core
QT       += network
QT       -= gui

TARGET   = qtTCPservertest
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
    theserver.cpp

HEADERS += \
    theserver.h

theServer.h

代码语言:javascript
复制
#ifndef THESERVER_H
#define THESERVER_H

#include <QTcpServer>
#include <stdio.h>


class theServer : public QTcpServer{
    Q_OBJECT
public:
    theServer();
    ~theServer();
    void goOnline();
};

#endif // THESERVER_H

theServer.cpp

代码语言:javascript
复制
#include "theserver.h"
theServer::theServer()
{
}

theServer::~theServer()
{
}

void theServer::goOnline()
{
       bool status = false;
       unsigned int portNum = 5200;

       status = this->listen(QHostAddress::Any, portNum );

       // Check, if the server did start correctly or not
       if( status == true )
           printf("Server up\n");
       else
           printf("Server down\n");
}

main.cpp

代码语言:javascript
复制
#include <QCoreApplication>
#include <stdio.h>
#include "theserver.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    printf("Test\n");
    theServer* aServer = new theServer();
    aServer->goOnline();
    aServer->~theServer();

    return a.exec();
}

有谁知道我哪里出了问题吗?因为没有错误,我完全没有头绪。它不打印任何东西,它只是告诉我按下任何键关闭窗口,就好像它像往常一样结束了。

谢谢你的建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-15 14:08:11

下面是为我编译和工作的代码(QT5.5):

TheServer.h

代码语言:javascript
复制
#ifndef THESERVER_H
#define THESERVER_H

#include <QTcpServer>

class TheServer : public QTcpServer
{
    Q_OBJECT
public:
    TheServer(QObject *pParent = nullptr);
    void goOnline();
};

#endif // THESERVER_H

TheServer.cpp

代码语言:javascript
复制
#include <QDebug>
#include "TheServer.h"

TheServer::TheServer(QObject *pParent)
    : QTcpServer(pParent)
{
}

void TheServer::goOnline()
{
    bool status = listen(QHostAddress::Any, 5200);

    if (status) {
        qDebug() << "Server up";
    } else {
        qDebug() << "Server down";
    }
}

main.cpp

代码语言:javascript
复制
#include <QCoreApplication>
#include "TheServer.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    TheServer server;
    server.goOnline();

    return a.exec();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34270960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档