当QTcpServer被调用时,Programm崩溃 [英] Programm crashes when QTcpServer is called

查看:132
本文介绍了当QTcpServer被调用时,Programm崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying a very, very simple QT networking program. For some reason it crashes when executing without any error message, since it's not printing out any of the outputs to the command line as expected. Here's the code:

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 >

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

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

main.cpp

and the main.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();
}

有人有想法,我哪里错了?因为没有错误,我总是无能为力。它只是不打印出任何东西,它只是告诉我打任何键来关闭窗口,好像它像往常一样结束。

Has anyone an idea, where I went wrong? Since there is no error I'm total clueless. It just doesn't print out anything, it just tells me to hit any key to close the window, as if it came to an end as usual.

感谢任何

推荐答案

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

Here is the code that compiles and works for me (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

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 / p>

main.cpp

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

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

    TheServer server;
    server.goOnline();

    return a.exec();
}

这篇关于当QTcpServer被调用时,Programm崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆