Multithreading. QTCPServer does not emit the newConnection() signal

  • I can't connect to the server side, the newConnection signal is not being emitted. If i run this in a single thread (main thread) then everything works ok.
    by using netstat -a in cmd I checked that the sockets are listening for a connection and I could connect to socket via the telnet command, but after I call nentstat -a again I see that the socket to which I have connected is in two states Connnection: ESTABLISHED and Connection: LISTENING, how a socket could be in two states? and why qt does not emit the newConnection signal.

    void TCPSocket::run(){
        socket = new QTcpSocket;
        server = new QTcpServer;
        emit msg("Itializing server side...");
        start_ = false;
        if(server->isListening() ){
    void TCPSocket::ListenS(){
            emit msg("Server is listening...");
            qDebug()<<"Server is listening";
            emit msg("Server cannot be started...");
            qDebug()<<"Server cannot be started"<<server->errorString();
    in MainWindow ctr;
    for(int i=0;i<4;i++){
            socketC = new TCPSocket;
            port_struct = new PORT_;
            port_struct->locked = true;

  • @mandruk1331

    Not sure if you are using the right terminology.

    However, when you have QTcpServer, it is listening on specified ports. Those you should as listening. When a client connect, QTcpServer gives you socket for the communication. However, if you have not limited the number of connetions through the server, it wil continue to listen on that port.

    What does the output of your application tell you?
    Any warning that you are doing something you should not?

  • @koahnig that's the problem the application has zero output that is why I can't understand what is the problem. When I use the netstat -a command in cmd I can see that the socket is listening for connection, and when I connect to it, the netstat -a shows that connection has been established. And there are no warnings.
    The new connection function:

    void TCPSocket::newConnectionS(){
        emit users("Test user");
        emit msg("New user connected...");
        socket = server->nextPendingConnection();
            emit msg("Socket has been opened...");
            qDebug()<<"Socket is open";
            emit msg("Socket cannot be opened...");
            qDebug()<<"Socket cannot be opened"<<socket->errorString();

    If you want I can put the server code on a GitHub account because I really don't understand what is the problem. No errors no warnings nothing.

  • Lifetime Qt Champion


    Your thread doesn't have an event loop just an infinite blocking loop which is not a good idea in the case of QTcpServer.

    If you really want to go that way, you should remove that loop and just call exec after you started your server.

    I'd recommend also taking a look at the threaded version of the Fortune examples in Qt's documentation.

  • @SGaist Thank you a lot! It worked.

Log in to reply

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