Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpServer not listening on port
Qt 6.11 is out! See what's new in the release blog

QTcpServer not listening on port

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 933 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dij0nmustard
    wrote on last edited by
    #1

    Hi! I'm fairly new to QTcpServer (and qt in general) and have been following a simple tutorial to create a server. However, when I run my program in qt creator and use "netstat -a" in command prompt, I don't see the port (1234) open. I also tried using telnet 127.0.0.1 1234, and get an error saying "Connect Failed". I can ping 127.0.0.1 and get packets back successfully. Any advice? I'm unsure if there is a firewall issue or if my program is just struggling to open port 1234. Attached is my code. Any advice would be greatly appreciated!

    I plan eventually to use two raspberry pi's to control motors, but that part of the code in main.cpp can be ignored.

    myserver.cpp

    #include <QtNetwork>
    #include <QAbstractSocket>
    #include "myserver.h"
    
    myServer::myServer(QObject *parent) :
        QObject(parent)
    {
    
        server = new QTcpServer(this);
        connect(server, &QTcpServer::newConnection, this, &myServer::newConnection);
    
        if(!server->listen(QHostAddress("127.0.0.1"), 1234))
        {
            qDebug() << "Server could not Start!";
        }
        else {
            qDebug() << "Server Started!";
        }
    }
    
    
    void myServer::newConnection()
    {
        QTcpSocket *socket = server->nextPendingConnection();
        socket->write("hullo client\r\n");
        socket->flush();
        socket->waitForBytesWritten(3000);
        socket->close();
    }
    

    myserver.h

    #ifndef MYSERVER_H
    #define MYSERVER_H
    #include <QtNetwork>
    #include <QDebug>
    
    
    class myServer: public QObject
    {
    
        Q_OBJECT
    
    public:
        explicit myServer(QObject *parent = 0); 
    
    public slots:
        void newConnection();
        void handleReadyRead();
    
    private:
        QTcpServer *server;
    
    };
    
    #endif // MYSERVER_H
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <myserver.h>
    #include <clientsocket.h>
    
    //Define Roles here.
    #define ROLE_MOTOR_CONTROLLER 1
    #define ROLE_COMMAND_SENDER 2
    
    //#define CURRENT_ROLE ROLE_COMMAND_SENDER
    #define CURRENT_ROLE ROLE_MOTOR_CONTROLLER
    
    //Use above line to change Motor Controller (server, RPI 1) or Command Sender (client, RPI 2).
    
    int main(int argc, char *argv[])
    {
    #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
    
        QGuiApplication app(argc, argv);
    
        if (CURRENT_ROLE == ROLE_MOTOR_CONTROLLER) {
            // Initialize and start the server (Motor Controller)
            myServer mServer;
            // Create an instance of MotorController and start the Qt event loop
        } else if (CURRENT_ROLE == ROLE_COMMAND_SENDER) {
            // Initialize and start the socket (Command Sender)
    
            clientSocket mClientSocket;
            mClientSocket.Test();
            // Create an instance of CommandSender and start the Qt event loop
        }
    
        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Please re-read your main function and take into account the lifetime of the objects you create there.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • D Offline
        D Offline
        dij0nmustard
        wrote on last edited by
        #3

        @SGaist

        Thanks for your answer! Can I ask you to clarify what you mean here?

        When I am initializing mServer in main.cpp, is there something later on that destroys it? Do I need a while loop to maintain it?

        SGaistS 1 Reply Last reply
        0
        • D dij0nmustard

          @SGaist

          Thanks for your answer! Can I ask you to clarify what you mean here?

          When I am initializing mServer in main.cpp, is there something later on that destroys it? Do I need a while loop to maintain it?

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4
          if (CURRENT_ROLE == ROLE_MOTOR_CONTROLLER) {
                  myServer mServer;   
              } /* mServer is destroyed here*/ else {
                ... 
              }
          

          That said, why not have real separated applications for your server and client ? You seem to try to shove everything into a single file which is a bad idea.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply
          1
          • SGaistS SGaist
            if (CURRENT_ROLE == ROLE_MOTOR_CONTROLLER) {
                    myServer mServer;   
                } /* mServer is destroyed here*/ else {
                  ... 
                }
            

            That said, why not have real separated applications for your server and client ? You seem to try to shove everything into a single file which is a bad idea.

            D Offline
            D Offline
            dij0nmustard
            wrote on last edited by
            #5

            @SGaist

            Oh I see! Thank you, that was helpful. And you bring up a great point - I'll separate them into two applications.

            Thanks!

            1 Reply Last reply
            0
            • D dij0nmustard has marked this topic as solved on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved