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. writing to QTcpSocket does not always emit readyRead signal on opposite QTcpSocket

writing to QTcpSocket does not always emit readyRead signal on opposite QTcpSocket

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcpserverqtcpsocketsignal&slotqt 5.7signals & slots
3 Posts 3 Posters 2.0k 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.
  • CybeXC Offline
    CybeXC Offline
    CybeX
    wrote on last edited by
    #1

    I have been stuck on this for the past 5 days, I have no idea how to proceed.

    Overview:

    I have a client UI which interacts with a data handler library, and the data handler library utilizes a network manager library, which is where my problem lies.

    More Info

    Firstly, QT provides a basic example for interactions between a QTcpServer (Fortune Server)and a QTcpSocket (Fortune Client).

    I thus implemented this code into an extremely basic example of my own, which works like a charm and has no issues.

    My own adaption of fortune client and server for the record (basic)

    Quick Explaination:

    Server application runs, click on start server, then on the client side, enter text in field and click connect to server and text is displayed, easy!

    Problem:

    Implementing the code above into my network manager library, does not fire the QTcpSocket::readyRead() in the server application above.

    It connects to the server, where the QTcpServer::newConnection() is fired, as expected, straight after which the client writes to the socket but the readyRead() on the server socket does not fire, however in the example given it does.

    Note:
    The same port and ip address is used in this server-client application example and my current application, and the server is also running.

    Further Information:

    From the above code, I copied over directly from the client. Only 2 things were changed/modified:

    • String that is sent to server
    • return types for method

    This was copied into my network mannager ::write() method. When running my application, and instance of QMainWindow is passed via data handler class and creates an instance of my network manager class which inherits QObject and implements the Q_OBJECT macro.

    Code Examples:

    //client_UI Class (snippet):

    data_mananger *dman = new data_mananger(this);                //this -> QMainWindow
    ReturnObject r = dman->NET_AuthenticateUser_GetToken(Query);
    

    //data_manager library (snippet)

    data_mananger::data_mananger(QObject *_parent) :
        parent(_parent)
    {}
    
    ReturnObject data_mananger::NET_AuthenticateUser_GetToken(QString Query){
        //Query like "AUTH;U=xyz@a;P=1234"
    
        //convert query string to char
            QByteArray ba = Query.toLatin1();
    
        //send query and get QList return
            ReturnCode rCode = networkManager.write(ba);
    
        //...
    }
    

    //netman library (snippet)

    //.h
    
    class NETMANSHARED_EXPORT netman : public QObject
    {
        Q_OBJECT
    public
        netman();
        netman(QObject *_parent);
        //...
    
    private:
        QTcpSocket *tcp_con;
        //...
    };
    

    //cpp

    netman::netman(QObject *_parent) :
        parent(_parent)
    {
        tcp_con = new QTcpSocket(parent);
    }
    
            return;
        }
        serverIP.setAddress(serverInfo.addresses().first().toIPv4Address());
    }
    
    ReturnCode netman::write(QByteArray message, int portNumber){
    
        tcp_con->connectToHost(QHostAddress("127.0.0.1"), 5000);
    
        if (!tcp_con->waitForConnected())
        {
            qDebug(log_lib_netman_err) << "Unable to connect to server";
            return ReturnCode::FailedConnecting;
        }
    
        if (!tcp_con->isValid()) {
            qDebug(log_lib_netman_err) << "tcp socket invalid";
            return ReturnCode::SocketError;
        }
    
        if (!tcp_con->isOpen()) {
            qDebug(log_lib_netman_err) << "tcp socket not open";
            return ReturnCode::SocketError;
        }
    
        //    QByteArray block(message);
        QByteArray block;
        QDataStream out(&block,QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_4_0);
    
        out << QString("Hello world");
    
        if (!tcp_con->write(block)){
            qDebug(log_lib_netman_err) << "Unable to send data to server";
            return ReturnCode::WriteFailed;
        }
        else{
            qDebug(log_lib_netman_info) << "Data block sent";
            return ReturnCode::SentSuccess;
        }
    }
    

    Conclusion:

    The core code of the client side has been fully implemented, yet I cannot see why this error occurs.

    I would very much appreciate help/advice!

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

      Hi,

      Did you check that you are not sending an empty QByteArray ?

      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
      0
      • CybeXC CybeX

        I have been stuck on this for the past 5 days, I have no idea how to proceed.

        Overview:

        I have a client UI which interacts with a data handler library, and the data handler library utilizes a network manager library, which is where my problem lies.

        More Info

        Firstly, QT provides a basic example for interactions between a QTcpServer (Fortune Server)and a QTcpSocket (Fortune Client).

        I thus implemented this code into an extremely basic example of my own, which works like a charm and has no issues.

        My own adaption of fortune client and server for the record (basic)

        Quick Explaination:

        Server application runs, click on start server, then on the client side, enter text in field and click connect to server and text is displayed, easy!

        Problem:

        Implementing the code above into my network manager library, does not fire the QTcpSocket::readyRead() in the server application above.

        It connects to the server, where the QTcpServer::newConnection() is fired, as expected, straight after which the client writes to the socket but the readyRead() on the server socket does not fire, however in the example given it does.

        Note:
        The same port and ip address is used in this server-client application example and my current application, and the server is also running.

        Further Information:

        From the above code, I copied over directly from the client. Only 2 things were changed/modified:

        • String that is sent to server
        • return types for method

        This was copied into my network mannager ::write() method. When running my application, and instance of QMainWindow is passed via data handler class and creates an instance of my network manager class which inherits QObject and implements the Q_OBJECT macro.

        Code Examples:

        //client_UI Class (snippet):

        data_mananger *dman = new data_mananger(this);                //this -> QMainWindow
        ReturnObject r = dman->NET_AuthenticateUser_GetToken(Query);
        

        //data_manager library (snippet)

        data_mananger::data_mananger(QObject *_parent) :
            parent(_parent)
        {}
        
        ReturnObject data_mananger::NET_AuthenticateUser_GetToken(QString Query){
            //Query like "AUTH;U=xyz@a;P=1234"
        
            //convert query string to char
                QByteArray ba = Query.toLatin1();
        
            //send query and get QList return
                ReturnCode rCode = networkManager.write(ba);
        
            //...
        }
        

        //netman library (snippet)

        //.h
        
        class NETMANSHARED_EXPORT netman : public QObject
        {
            Q_OBJECT
        public
            netman();
            netman(QObject *_parent);
            //...
        
        private:
            QTcpSocket *tcp_con;
            //...
        };
        

        //cpp

        netman::netman(QObject *_parent) :
            parent(_parent)
        {
            tcp_con = new QTcpSocket(parent);
        }
        
                return;
            }
            serverIP.setAddress(serverInfo.addresses().first().toIPv4Address());
        }
        
        ReturnCode netman::write(QByteArray message, int portNumber){
        
            tcp_con->connectToHost(QHostAddress("127.0.0.1"), 5000);
        
            if (!tcp_con->waitForConnected())
            {
                qDebug(log_lib_netman_err) << "Unable to connect to server";
                return ReturnCode::FailedConnecting;
            }
        
            if (!tcp_con->isValid()) {
                qDebug(log_lib_netman_err) << "tcp socket invalid";
                return ReturnCode::SocketError;
            }
        
            if (!tcp_con->isOpen()) {
                qDebug(log_lib_netman_err) << "tcp socket not open";
                return ReturnCode::SocketError;
            }
        
            //    QByteArray block(message);
            QByteArray block;
            QDataStream out(&block,QIODevice::WriteOnly);
            out.setVersion(QDataStream::Qt_4_0);
        
            out << QString("Hello world");
        
            if (!tcp_con->write(block)){
                qDebug(log_lib_netman_err) << "Unable to send data to server";
                return ReturnCode::WriteFailed;
            }
            else{
                qDebug(log_lib_netman_info) << "Data block sent";
                return ReturnCode::SentSuccess;
            }
        }
        

        Conclusion:

        The core code of the client side has been fully implemented, yet I cannot see why this error occurs.

        I would very much appreciate help/advice!

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        Isn't this the same issue as here https://forum.qt.io/topic/75821/qabstractsocket-unknownsocketerror-provides-errorstring-of-unknownerror?

        Read and abide by the Qt Code of Conduct

        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