QTcpServer - program finishes after listen

  • 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

    [code]QT       += core network
    QT       -= gui

    TARGET = server
    CONFIG   += console
    CONFIG   -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp

    HEADERS +=

    [code]#ifndef MYSERVER_H
    #define MYSERVER_H

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

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

       QTcpServer *server;

    #endif // MYSERVER_H

    [code]#include "myserver.h"

    MyServer::MyServer(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");
           printf("Server started\r\n");

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

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




    [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]open 1234[/code]

  • Problem suddenly was solved. I did nothing ^_^

  • 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.

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.