c++ - 调用 QTcpServer 时程序崩溃

标签 c++ qt syntax-error runtime-error qtcpserver

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

qtTCPservertest.pro

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

#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

#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");
}

ma​​in.cpp

#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();
}

有没有人知道我哪里出错了?由于没有错误,我完全无能为力。它只是不打印任何东西,它只是告诉我按任意键关闭窗口,就好像它像往常一样结束了。

感谢您的任何建议。

最佳答案

这是为我编译和工作的代码(Qt 5.5):

TheServer.h

#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

#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

#include <QCoreApplication>
#include "TheServer.h"

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

    TheServer server;
    server.goOnline();

    return a.exec();
}

关于c++ - 调用 QTcpServer 时程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34270960/

相关文章:

c++ - Qt5 中不显示 SVG 图标

Qt Quick 模拟器层崩溃

syntax-error - OCaml : and keyword syntax error

bash - bash脚本: syntax error near unexpected token `done'

c++ - 以 FLOPS 估算 GPU 的效率(CUDA SAMPLES)

c++ - 来自其他线程的 select() 不处理传入数据

c++ - OpenCV 拼接结果的尺寸不稳定

c++ - 如何在 Windows 上获取插入的 USB 设备的供应商 ID 和产品 ID

python - 在Pandas Dataframe中获取语法错误

c++ - 为什么使用局部结构作为 STL 函数参数的代码不能在 g++ 中编译?