1

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

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

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.

Thanks for any advise.

user3085931
  • 1,757
  • 4
  • 29
  • 55
  • Did you try to run it in debug? – cen Dec 14 '15 at 15:40
  • @cen right now I'm having problems in using gdb with Qt/Creator and since I believe fixing this would be a bigger job, I couldn't debug it (if you mean e.g.: `gdb` by that) – user3085931 Dec 14 '15 at 15:43
  • 2
    I would suggest you fix debugger in your Qt Creator because at least then you will know exactly where the program crashes. And once you know that it should be easier. If "Test" is not printed you have bigger problems that networking. – cen Dec 14 '15 at 15:47
  • 2
    Your theServer class has to be a QObject (Q_OBJECT macro is missing) – Archie Dec 14 '15 at 15:56
  • @Archie yeah that's right but it's still the same. I tried an alternative: my Class inherits just from "public QObject" and I'm declaring a server like `QTcpServer* aServer` until here everything runs smoothly, if i insert the initializiation that error comes back `aServer = new QTcpServer(this)` and id doesn't matter if call that constructor voidly. That's why I still believe it's the network problem – user3085931 Dec 15 '15 at 13:35
  • @user3085931 I've just compiled the code it runs fine, no crashing. Be sure to put Q_OBJECT macro in theServer class declaration and re-run the qmake. Also don't use new theServer(), but allocate it on the stack (you are not deleting it!). – Archie Dec 15 '15 at 13:52
  • @Archie well that's even worse, have a look at my updated code. Are you sure we have the same code by now ? And I'm using a `Qt 4.81` are you using a similiar one or >QT5 ? – user3085931 Dec 15 '15 at 14:01
  • @user3085931 You are using destructor wrong. Please see my answer. Tried with Qt 5.5. – Archie Dec 15 '15 at 14:09

1 Answers1

1

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

#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();
}
Archie
  • 2,644
  • 21
  • 21
  • First of all: thanks, for you patient and help. This isn't really solving my problem but it sure shows that I'm on the right track and the code is alright. I didn't expect this but now I believe that there is a problem with the QTNetwork library, maybe missing or incompatible. So I have to debug the QT-setup I was given here, since it's not a common one off the shelve – user3085931 Dec 15 '15 at 14:46
  • 1
    @user3085931 Make sure you load correct Qt libraries (e.g. if you have several Qt installations or some Qt-dependent program puts itself into the PATH). On Windows you can use dependency walker (http://www.dependencywalker.com) to see what DLLs are actually loaded when launching your executable. – Archie Dec 15 '15 at 15:01