Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multithreading. QTCPServer does not emit the newConnection() signal
Forum Updated to NodeBB v4.3 + New Features

Multithreading. QTCPServer does not emit the newConnection() signal

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.0k Views 2 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.
  • mandruk1331M Offline
    mandruk1331M Offline
    mandruk1331
    wrote on last edited by mandruk1331
    #1

    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.
    Update:
    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(){
    
      while(1){
        if(start_){
        socket = new QTcpSocket;
        server = new QTcpServer;
        connect(server,SIGNAL(newConnection()),this,SLOT(newConnectionS()));
        connect(socket,SIGNAL(disconnected()),this,SLOT(Disconnected()));
        connect(&timer_,SIGNAL(timeout()),this,SLOT(Disconnected()));
        emit msg("Itializing server side...");
        start_ = false;
        }
        if(server->isListening() ){
    
        }else{
            this->ListenS();
            //server->waitForNewConnection();
        }
    }
    
    }
    
    void TCPSocket::ListenS(){
        if(server->listen(QHostAddress::LocalHost,PORT_NUM)){
            emit msg("Server is listening...");
            qDebug()<<"Server is listening";
        }
        else{
            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;
            //connect(this,SIGNAL(startThread()),socketC,SLOT(StartThread()));
            //connect(socketC,SIGNAL(sendImage(QImage&)),this,SLOT(recImg(QImage&)));
            connect(socketC,SIGNAL(msg(QString)),this,SLOT(Log(QString)));
            connect(socketC,SIGNAL(users(QString)),this,SLOT(Users(QString)));
            connect(socketC,SIGNAL(idDisconnected(int)),SLOT(UpdateList(int)));
            port_struct = new PORT_;
            port_struct->locked = true;
            socketC->setPORT(DEFAULT_PORT+i);
            socketC->setId(i);
            SocketInst.push_back(socketC);
            PORTS_NUM.push_back(port_struct);
            SocketInst.at(i)->start();
        }
    

    Mandruk1331

    K 1 Reply Last reply
    0
    • mandruk1331M mandruk1331

      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.
      Update:
      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(){
      
        while(1){
          if(start_){
          socket = new QTcpSocket;
          server = new QTcpServer;
          connect(server,SIGNAL(newConnection()),this,SLOT(newConnectionS()));
          connect(socket,SIGNAL(disconnected()),this,SLOT(Disconnected()));
          connect(&timer_,SIGNAL(timeout()),this,SLOT(Disconnected()));
          emit msg("Itializing server side...");
          start_ = false;
          }
          if(server->isListening() ){
      
          }else{
              this->ListenS();
              //server->waitForNewConnection();
          }
      }
      
      }
      
      void TCPSocket::ListenS(){
          if(server->listen(QHostAddress::LocalHost,PORT_NUM)){
              emit msg("Server is listening...");
              qDebug()<<"Server is listening";
          }
          else{
              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;
              //connect(this,SIGNAL(startThread()),socketC,SLOT(StartThread()));
              //connect(socketC,SIGNAL(sendImage(QImage&)),this,SLOT(recImg(QImage&)));
              connect(socketC,SIGNAL(msg(QString)),this,SLOT(Log(QString)));
              connect(socketC,SIGNAL(users(QString)),this,SLOT(Users(QString)));
              connect(socketC,SIGNAL(idDisconnected(int)),SLOT(UpdateList(int)));
              port_struct = new PORT_;
              port_struct->locked = true;
              socketC->setPORT(DEFAULT_PORT+i);
              socketC->setId(i);
              SocketInst.push_back(socketC);
              PORTS_NUM.push_back(port_struct);
              SocketInst.at(i)->start();
          }
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by koahnig
      #2

      @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?

      Vote the answer(s) that helped you to solve your issue(s)

      mandruk1331M 1 Reply Last reply
      1
      • K koahnig

        @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?

        mandruk1331M Offline
        mandruk1331M Offline
        mandruk1331
        wrote on last edited by mandruk1331
        #3

        @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...");
            qDebug()<<"Connected";
            //socket->open(QIODevice::ReadWrite);
            socket = server->nextPendingConnection();
            connect(socket,SIGNAL(readyRead()),this,SLOT(newImageReceived()));
            if(socket->isOpen()){
                emit msg("Socket has been opened...");
                qDebug()<<"Socket is open";
            }else
            {
                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.

        Mandruk1331

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • mandruk1331M Offline
            mandruk1331M Offline
            mandruk1331
            wrote on last edited by
            #5

            @SGaist Thank you a lot! It worked.

            Mandruk1331

            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 Extensions
            • Unsolved