<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QTcpServer not listening on port]]></title><description><![CDATA[<p dir="auto">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!</p>
<p dir="auto">I plan eventually to use two raspberry pi's to control motors, but that part of the code in main.cpp can be ignored.</p>
<p dir="auto"><strong>myserver.cpp</strong></p>
<pre><code>#include &lt;QtNetwork&gt;
#include &lt;QAbstractSocket&gt;
#include "myserver.h"

myServer::myServer(QObject *parent) :
    QObject(parent)
{

    server = new QTcpServer(this);
    connect(server, &amp;QTcpServer::newConnection, this, &amp;myServer::newConnection);

    if(!server-&gt;listen(QHostAddress("127.0.0.1"), 1234))
    {
        qDebug() &lt;&lt; "Server could not Start!";
    }
    else {
        qDebug() &lt;&lt; "Server Started!";
    }
}


void myServer::newConnection()
{
    QTcpSocket *socket = server-&gt;nextPendingConnection();
    socket-&gt;write("hullo client\r\n");
    socket-&gt;flush();
    socket-&gt;waitForBytesWritten(3000);
    socket-&gt;close();
}
</code></pre>
<p dir="auto"><strong>myserver.h</strong></p>
<pre><code>#ifndef MYSERVER_H
#define MYSERVER_H
#include &lt;QtNetwork&gt;
#include &lt;QDebug&gt;


class myServer: public QObject
{

    Q_OBJECT

public:
    explicit myServer(QObject *parent = 0); 

public slots:
    void newConnection();
    void handleReadyRead();

private:
    QTcpServer *server;

};

#endif // MYSERVER_H
</code></pre>
<p dir="auto"><strong>main.cpp</strong></p>
<pre><code>#include &lt;QGuiApplication&gt;
#include &lt;QQmlApplicationEngine&gt;
#include &lt;myserver.h&gt;
#include &lt;clientsocket.h&gt;

//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 &lt; 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(&amp;engine, &amp;QQmlApplicationEngine::objectCreated,
                     &amp;app, [url](QObject *obj, const QUrl &amp;objUrl) {
        if (!obj &amp;&amp; url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/148753/qtcpserver-not-listening-on-port</link><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Apr 2026 09:50:14 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/148753.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 30 Aug 2023 17:53:18 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QTcpServer not listening on port on Wed, 30 Aug 2023 18:54:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a></p>
<p dir="auto">Oh I see! Thank you, that was helpful. And you bring up a great point - I'll separate them into two applications.</p>
<p dir="auto">Thanks!</p>
]]></description><link>https://forum.qt.io/post/770908</link><guid isPermaLink="true">https://forum.qt.io/post/770908</guid><dc:creator><![CDATA[dij0nmustard]]></dc:creator><pubDate>Wed, 30 Aug 2023 18:54:07 GMT</pubDate></item><item><title><![CDATA[Reply to QTcpServer not listening on port on Wed, 30 Aug 2023 18:47:21 GMT]]></title><description><![CDATA[<pre><code>if (CURRENT_ROLE == ROLE_MOTOR_CONTROLLER) {
        myServer mServer;   
    } /* mServer is destroyed here*/ else {
      ... 
    }
</code></pre>
<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/770905</link><guid isPermaLink="true">https://forum.qt.io/post/770905</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 30 Aug 2023 18:47:21 GMT</pubDate></item><item><title><![CDATA[Reply to QTcpServer not listening on port on Wed, 30 Aug 2023 18:40:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a></p>
<p dir="auto">Thanks for your answer! Can I ask you to clarify what you mean here?</p>
<p dir="auto">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?</p>
]]></description><link>https://forum.qt.io/post/770904</link><guid isPermaLink="true">https://forum.qt.io/post/770904</guid><dc:creator><![CDATA[dij0nmustard]]></dc:creator><pubDate>Wed, 30 Aug 2023 18:40:39 GMT</pubDate></item><item><title><![CDATA[Reply to QTcpServer not listening on port on Wed, 30 Aug 2023 18:15:00 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to devnet,</p>
<p dir="auto">Please re-read your main function and take into account the lifetime of the objects you create there.</p>
]]></description><link>https://forum.qt.io/post/770897</link><guid isPermaLink="true">https://forum.qt.io/post/770897</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 30 Aug 2023 18:15:00 GMT</pubDate></item></channel></rss>