调用QTcpServer时程序崩溃
Programm crashes when QTcpServer is called
我正在尝试一个非常非常简单的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
Server.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
服务器.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 upn");
else
printf("Server downn");
}
和main.cpp
#include <QCoreApplication>
#include <stdio.h>
#include "theserver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
printf("Testn");
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();
}
相关文章:
- 提升 ASIO Async_receive崩溃程序
- 类指针方法崩溃程序
- get_body来自 IHTMLDocument2 崩溃程序
- 是否有可能存在不会崩溃程序的内存问题
- 调试运行时出现奇怪的崩溃程序(Eclipse C++)
- 儿童对话框 - setWindowTexta或sendmessagea崩溃程序-MFC
- 顶点阵列GLFW崩溃C 程序
- 将值分配给Float数据类型崩溃程序
- OpenCV-将变量添加到类成员崩溃程序
- boost::interprocess::managed_shared_memory 崩溃程序
- 将枚举值分配给整数崩溃程序
- SDL 1.2 -> SDL 2.0(崩溃程序)
- 空析构函数崩溃程序:C++
- 冒泡排序崩溃程序c++
- Ofstream关闭崩溃程序
- 删除字符* 崩溃程序
- c++矢量擦除崩溃程序
- Lua_getglobal崩溃程序
- 通过Copy Constructor创建的e2打印崩溃程序
- RNG崩溃c++程序