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. QTcpSokcet connected() signal not fired
Forum Updated to NodeBB v4.3 + New Features

QTcpSokcet connected() signal not fired

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcpsoketsignalslotconnect
8 Posts 2 Posters 4.9k Views
  • 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.
  • divergerD Offline
    divergerD Offline
    diverger
    wrote on last edited by diverger
    #1

    I'm working on a project with Qt on windows. I connected the QTcpSocket's connected() signal to a slot, but after call "connectToHost', my slot won't called, so I doubt the 'connected()' signal is not fired or delayed.

    
        proc()
        {
                ....
                connect(_socket, SIGNAL(connected()), this, SLOT(socket_connected()), Qt::DirectConnection);
        
                _socket->connectToHost(tester->ip_info().ip_addr.toString(), TCP_COMM_PORT, QTcpSocket::ReadWrite, QTcpSocket::IPv4Protocol); 
        ...
        }
    

    Anyone can give a help? Where my code is wrong?

    Best reards.

    jsulmJ 1 Reply Last reply
    0
    • divergerD diverger

      I'm working on a project with Qt on windows. I connected the QTcpSocket's connected() signal to a slot, but after call "connectToHost', my slot won't called, so I doubt the 'connected()' signal is not fired or delayed.

      
          proc()
          {
                  ....
                  connect(_socket, SIGNAL(connected()), this, SLOT(socket_connected()), Qt::DirectConnection);
          
                  _socket->connectToHost(tester->ip_info().ip_addr.toString(), TCP_COMM_PORT, QTcpSocket::ReadWrite, QTcpSocket::IPv4Protocol); 
          ...
          }
      

      Anyone can give a help? Where my code is wrong?

      Best reards.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @diverger You should connect http://doc.qt.io/qt-5/qabstractsocket.html#error-1 signal to a slot and print the error there to check whether any error occurs. And you should check whether the connection for connected actually succeeded:

      qDebug() << connect(_socket, SIGNAL(connected()), this, SLOT(socket_connected()), Qt::DirectConnection);
      

      should output true.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      divergerD 1 Reply Last reply
      0
      • jsulmJ jsulm

        @diverger You should connect http://doc.qt.io/qt-5/qabstractsocket.html#error-1 signal to a slot and print the error there to check whether any error occurs. And you should check whether the connection for connected actually succeeded:

        qDebug() << connect(_socket, SIGNAL(connected()), this, SLOT(socket_connected()), Qt::DirectConnection);
        

        should output true.

        divergerD Offline
        divergerD Offline
        diverger
        wrote on last edited by
        #3

        @jsulm Thanks for the help. It return true. Is it mandatory to call

        _socket->waitForConnected(timeout);
        

        after 'connecToHost()' ? I find if I add the clause above, my slot is called. IMHO, 'waitForxxxxx' are for blocking operations, why it will affect the async-signals?

        Now, i'm a little confused about how to use the APIs Qt provided for sockets.

        jsulmJ 1 Reply Last reply
        0
        • divergerD diverger

          @jsulm Thanks for the help. It return true. Is it mandatory to call

          _socket->waitForConnected(timeout);
          

          after 'connecToHost()' ? I find if I add the clause above, my slot is called. IMHO, 'waitForxxxxx' are for blocking operations, why it will affect the async-signals?

          Now, i'm a little confused about how to use the APIs Qt provided for sockets.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @diverger Can you show more code? The whole method where you try to connect?
          _socket->waitForConnected(timeout) is not needed, it should work without.
          Did you try to connect the error signal as I suggested?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          divergerD 2 Replies Last reply
          0
          • jsulmJ jsulm

            @diverger Can you show more code? The whole method where you try to connect?
            _socket->waitForConnected(timeout) is not needed, it should work without.
            Did you try to connect the error signal as I suggested?

            divergerD Offline
            divergerD Offline
            diverger
            wrote on last edited by
            #5

            @jsulm I create a worker class and move it to a new thread. In the worker class's constructor, I connect the socket signals to the slots in the same class.

            	connect(_socket, SIGNAL(connected()), this, SLOT(socket_connected()));
            	connect(_socket, &QTcpSocket::readyRead, this, &XXXXX::read_ready);
            	connect(_socket, &QTcpSocket::bytesWritten, this, &XXXXX::bytes_written);
            
            

            And the main worker code is:

            void XXXXX::proc_start()
            {
            	CommProc::proc_start();
            
            	while (this->_is_enabled)
            	{
            		qDebug() << QThread::currentThreadId();
            
            		eth_comm_command_pkt_t command_pkt = {
            			MODEL_GUID,
            			0, 0
            		};
            
            		eth_comm_status_pkt_t status_pkt;
            
            		auto timeout = 5 * 1000;
            		
            		_socket_connected = false;
            		_socket->connectToHost(tester->ip_info().ip_addr.toString(), TCP_COMM_PORT, QTcpSocket::ReadWrite, QTcpSocket::IPv4Protocol);
            
            		if (!_socket->waitForConnected(timeout))
            		{
            			// error
            			emit socket_error(_socket->error(), _socket->errorString());
            			return;
            		}
            
            		while (!_socket_connected);
            		
            		// send command
            		_socket_bytes_written = 0;
            		command_pkt.command = 1;
            		qint64 bytes_written = _socket->write((char const *)&command_pkt, sizeof(command_pkt));
            
            		if (bytes_written < 0)
            		{
            			// error
            			emit socket_error(_socket->error(), _socket->errorString());
            			return;
            		}
            		
            		while (!_socket->waitForBytesWritten(timeout))
            		{
            			emit socket_error(_socket->error(), _socket->errorString());
            		}
            
            		while (_socket_bytes_written < sizeof(command_pkt))
            		{
            		}
            
            		// read status
            		if (!_socket->waitForReadyRead(timeout)) {
            			emit socket_error(_socket->error(), _socket->errorString());
            			return;
            		}
            
            		while (_socket_bytes_ready < sizeof(status_pkt))
            		{
            		}
            
            		_socket->read((char *)&status_pkt, sizeof(status_pkt));
            
            		if (status_pkt.status == status_pkt.STA_OK)
            		{
            			//
            			qDebug() << "Send command done";
            		}
            		else
            		{
            			qDebug() << "Command error!";
            		}
            	}
            }
            

            I've debuged it, the worker code indeed run in a new thread.

            jsulmJ 1 Reply Last reply
            0
            • jsulmJ jsulm

              @diverger Can you show more code? The whole method where you try to connect?
              _socket->waitForConnected(timeout) is not needed, it should work without.
              Did you try to connect the error signal as I suggested?

              divergerD Offline
              divergerD Offline
              diverger
              wrote on last edited by
              #6

              @jsulm I've noticed my worker main thread and the slots in the same thread, and the main thread is blocking, should it blocked the slots?

              1 Reply Last reply
              0
              • divergerD diverger

                @jsulm I create a worker class and move it to a new thread. In the worker class's constructor, I connect the socket signals to the slots in the same class.

                	connect(_socket, SIGNAL(connected()), this, SLOT(socket_connected()));
                	connect(_socket, &QTcpSocket::readyRead, this, &XXXXX::read_ready);
                	connect(_socket, &QTcpSocket::bytesWritten, this, &XXXXX::bytes_written);
                
                

                And the main worker code is:

                void XXXXX::proc_start()
                {
                	CommProc::proc_start();
                
                	while (this->_is_enabled)
                	{
                		qDebug() << QThread::currentThreadId();
                
                		eth_comm_command_pkt_t command_pkt = {
                			MODEL_GUID,
                			0, 0
                		};
                
                		eth_comm_status_pkt_t status_pkt;
                
                		auto timeout = 5 * 1000;
                		
                		_socket_connected = false;
                		_socket->connectToHost(tester->ip_info().ip_addr.toString(), TCP_COMM_PORT, QTcpSocket::ReadWrite, QTcpSocket::IPv4Protocol);
                
                		if (!_socket->waitForConnected(timeout))
                		{
                			// error
                			emit socket_error(_socket->error(), _socket->errorString());
                			return;
                		}
                
                		while (!_socket_connected);
                		
                		// send command
                		_socket_bytes_written = 0;
                		command_pkt.command = 1;
                		qint64 bytes_written = _socket->write((char const *)&command_pkt, sizeof(command_pkt));
                
                		if (bytes_written < 0)
                		{
                			// error
                			emit socket_error(_socket->error(), _socket->errorString());
                			return;
                		}
                		
                		while (!_socket->waitForBytesWritten(timeout))
                		{
                			emit socket_error(_socket->error(), _socket->errorString());
                		}
                
                		while (_socket_bytes_written < sizeof(command_pkt))
                		{
                		}
                
                		// read status
                		if (!_socket->waitForReadyRead(timeout)) {
                			emit socket_error(_socket->error(), _socket->errorString());
                			return;
                		}
                
                		while (_socket_bytes_ready < sizeof(status_pkt))
                		{
                		}
                
                		_socket->read((char *)&status_pkt, sizeof(status_pkt));
                
                		if (status_pkt.status == status_pkt.STA_OK)
                		{
                			//
                			qDebug() << "Send command done";
                		}
                		else
                		{
                			qDebug() << "Command error!";
                		}
                	}
                }
                

                I've debuged it, the worker code indeed run in a new thread.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @diverger You should remove all that while loops, because they block the event loop. Blocked event loop means: no signals can be emited/handled. I would suggest to take a look at Qt networking examples to see how Qt networking should be used. It is asynchronous event driven.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                divergerD 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @diverger You should remove all that while loops, because they block the event loop. Blocked event loop means: no signals can be emited/handled. I would suggest to take a look at Qt networking examples to see how Qt networking should be used. It is asynchronous event driven.

                  divergerD Offline
                  divergerD Offline
                  diverger
                  wrote on last edited by
                  #8

                  @jsulm Thanks, I'll give it a try.

                  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