Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpServer - program finishes after listen
Qt 6.12 LTS is out! See what's new in the release blog

QTcpServer - program finishes after listen

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.0k Views 1 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello all.

    I'm new with Qt so please excuse me for a simple question.
    I need to create a simple tcp client-server application. I found a "youtube video":http://www.youtube.com/watch?v=BSdKkZNEKlQ explaining how to use QTcpServer. Problem is that my program finishes before client doesnt manage to connect.
    What is the problem? I'm not well with streams and threads, does listen() method start a new thread?
    Thank you for your answers

    server.pro
    [code]QT       += core network
    QT       -= gui

    TARGET = server
    CONFIG   += console
    CONFIG   -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp
       myserver.cpp

    HEADERS +=
       myserver.h[/code]

    myserver.h
    [code]#ifndef MYSERVER_H
    #define MYSERVER_H

    #include <QObject>
    #include <QDebug>
    #include <QTcpServer>
    #include <QTcpSocket>

    class MyServer : public QObject
    {
       Q_OBJECT
    public:
       explicit MyServer(QObject *parent = 0);
       
    signals:
       
    public slots:
       void acceptConnection();

    private:
       QTcpServer *server;
       
    };

    #endif // MYSERVER_H
    [/code]

    myserver.cpp
    [code]#include "myserver.h"

    MyServer::MyServer(QObject *parent) :
       QObject(parent)
    {
       server = new QTcpServer(this);

    connect(server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));

    if( !server->listen(QHostAddress::Any,1234))
           printf("Server could not start\r\n");
       else
           printf("Server started\r\n");
    }

    void MyServer::acceptConnection()
    {
       QTcpSocket *socket = server->nextPendingConnection();

    socket->write("hello client\r\n");
       socket->flush();

    socket->waitForBytesWritten(3000);

    socket->close();

    }
    [/code]

    main.cpp
    [code]#include <QCoreApplication>
    #include <stdio.h>
    #include "myserver.h"

    int main(int argc, char * argv[])
    {
       QCoreApplication a(argc, argv);
       MyServer mServer;
       printf( "finished" );
       return a.exec();
    }
    [/code]

    telnet.exe
    [code]open 127.0.0.1 1234[/code]

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Problem suddenly was solved. I did nothing ^_^

      1 Reply Last reply
      0
      • C Offline
        C Offline
        code_fodder
        wrote on last edited by
        #3

        I would just note that I have found sometimes when developing I can get some build errors, then undo my changes and still have errors. Then when I delete all my objects / output files and do a full re-build the problem goes away.

        Sometimes I think the build process gets the files it needs to re-compile or not confused and this can cause strange issues...

        That's just a note, its not a concrete issue, just something I have observed.

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          When in doubt it is always a good thing to remove all object files and recompile. That's standard compilation mixup fix.
          Greetz

          Greetz, Jeroen

          1 Reply Last reply
          0

          • Login

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