QTcpServer not listening on port
-
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_Hmain.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(); } -
Hi and welcome to devnet,
Please re-read your main function and take into account the lifetime of the objects you create there.
-
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?
-
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?
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.
-
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.
Oh I see! Thank you, that was helpful. And you bring up a great point - I'll separate them into two applications.
Thanks!
-
D dij0nmustard has marked this topic as solved on