Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT app:exec is blocking the code.



  • Hi, I am creating a normal socket client communication (non-QT) in my DLL.
    From DLL i need to read and send the data to my QT GUI application.

    But when I am trying to read the data in my QT GUI application. I am able to read the content only before exec statement.

    Is there any way I can detect that data came from DLL to my QT GUI application and it can be read.
    Please note that there is no event loop running in my DLL which is loaded by non QT C++ application. I am using simple socket not QT Based socket.

    I need to update GUI if data come from DLL to my application using sockets also needs to data to send back from GUI to DLL application.

    But the app:exec event is blocking the flow. How I can achieve it?



  • @Ayush-Gupta
    So you may (will) need a QThread to communicate directly with your DLL, while running the Qt GUI in the main thread. Then you won't block. Use signals/slots to communicate between the DLL/worker/socket thread and the main UI.

    running in my DLL which is loaded by non QT C++ application

    Not sure what you mean here. You won't a have a non-Qt application running if you are in a Qt application, only one application (the Qt one). I'll assume you mean something more like a non-Qt library/code/API. Unless you really mean a separate application/program, in which case you would need some IPC with your Qt program, but I don't think you mean that as you talk about calling stuff in a DLL.



  • My DLL will be loaded by console application which is non QT application.

    DLL and QTGUI application should be communicate each other on different channels using sockets.

    so QTGUI needs to be updated once the data on channel is updated means data is written by DLL in socket port.

    and QT GUI should send the data on channel to DLL when some action is performed on UI. And that data will be received by DLL.



  • @Ayush-Gupta
    Fine, then forget even mentioning the word "DLL". You just (seem to) have a completely separate process you'd like to communicate with. There is no relationship between your GUI Qt application and the separate console process.

    So all you have here is two separate executables which wish to communicate via sockets. Your Qt UI app needs to use Qt asynchronous QTcpSocket signal/slot calls to send/receive data between itself and the separate app.

    The fact that the separate app is using its own synchronous sockets is neither here nor there.

    And I have no idea now what your "I am able to read the content only before exec statement" means. Assuming you are talking about QApplication::exec() in your Qt UI app, I don't know how you even got it talking to this separate process across sockets.



  • I am using simple c++ sockets (non-QT sockets) writing data to port and reading from it.
    Other application is not QT application (console application) hence I cannot use QTcpSocket.

    "I am able to read the content only before exec statement" means
    Before the exec statement I am able to read and write data into port using normal sockets .

    I thought to using normal sockets (non-qt based) since my other console application is non-qt application.

    I am not sure if I can use normal sockets on one side and QTcpSocket on other side to communicate.

    Will that work?

    Or any best way to communicate between non qt application and QT application ?


  • Lifetime Qt Champion

    @Ayush-Gupta said in QT app:exec is blocking the code.:

    hence I cannot use QTcpSocket.

    Why not? It's simple tcp ...



  • Hi,

    I am trying this on QT GUI app.

    Created one UDP socket class and initializes the port. Now I am trying to write the data using WriteDatagram function. Function returns value as successful (the number of bytes written)

    but when I am trying to read the data gram in my other application from same port using normal sockets I am not able read anything.

    On other hand if cach readyRead() signal I am able to read the data in same QT-GUI application.



  • @Ayush-Gupta
    Don't know, it's your other application's read code that is at issue. As we have said, Qt socket stuff is just standard, the fact that it comes from Qt or uses synchronous/asynchronous calls is not relevant. There is no "normal" vs "Qt" sockets.

    Purely at a guess, since you originally said your other app was doing TCP, now it;s not doing UDP, or it;s listening on the wrong port. Use WireShark or similar if you want to check what's being sent around.



  • Now I corrected the mistake the address given in sin_addr was wrong. I mentioned it now as
    127.0.0.1

    with this I am able to receive message from UdpSocket from QT application to normal socket in non-qt application but when I am trying to send message from my non-qt application to QT application signal readyRead not emitted and not able to read the data.



  • @Ayush-Gupta
    Then you are doing something wrong in your receiving side, or possibly at the sender side. What else do you want us to say? Simple sample code is given in https://doc.qt.io/qt-5/qudpsocket.html. You have bound to the port for receiving, haven't you?



  • @JonB I am pasting my code here

    UdpSocket.h

    #ifndef UDPSOCKET_H
    #define UDPSOCKET_H
    #include <QUdpSocket>
    #include <MMISimForm.h>
    
    class UdpSocket : public QObject
    {
    
        Q_OBJECT
    public:
           UdpSocket(QObject* parent, int portno, MMISimForm* simForm_);
           ~UdpSocket();
    
    private:
           int portNo_;
           MMISimForm* mmiSimForm_;
           QUdpSocket *socketUdp;
    
    public slots:
           void dataRead();
    
    //methods
    public:
           int sendData();
    
    };
    
    #endif // UDPSOCKET_H
    

    UdpSocket.cpp

    #include "UdpSocket.h"
    
    
    UdpSocket::UdpSocket(QObject *parent, int portno, MMISimForm* simForm_)
              :QObject(parent), portNo_(portno), mmiSimForm_(simForm_)
    {
    
        socketUdp = new QUdpSocket(this);
        socketUdp->bind(QHostAddress::LocalHost, portNo_);
        connect(socketUdp, SIGNAL(readyRead()), this, SLOT(dataRead()));
    }
    
    
    UdpSocket::~UdpSocket()
    {
    
    }
    
    void UdpSocket::dataRead()
    {
        QByteArray buffer;
        QHostAddress sender;
        quint16 senderPort;
        buffer.resize(socketUdp->pendingDatagramSize());
        int test = socketUdp->readDatagram(buffer.data(), buffer.size(),
                                            &sender, &senderPort);
        QString text;
        text.sprintf("Bytes Read %d", test);
        mmiSimForm_->logMessage(buffer);
    }
    
    int UdpSocket::sendData()
    {
         return socketUdp->writeDatagram("GetStream", QHostAddress::LocalHost,
                                   5000);
    }
    

    My non - qt application code

    ServerSocket.cpp

    #include "ServerSocket.h"
    #include "mmisimlogger.h"
    ServerSocket::ServerSocket()
    {
        WSAStartup(0x0101, &w) != 0;
        /* Open a datagram socket */
        sd = socket(AF_INET, SOCK_DGRAM, 0);
    
        /* Clear out server struct */
        memset((void *)&server, '\0', sizeof(struct sockaddr_in));
        memset((void *)&client, '\0', sizeof(struct sockaddr_in));
        server.sin_family = AF_INET;
        server.sin_port = htons(5000);
    
        /* Get host name of this computer */
        gethostname(host_name, sizeof(host_name));
        hp = gethostbyname(host_name);
    
         /*Assign the address
        server.sin_addr.S_un.S_un_b.s_b1 = hp->h_addr_list[0][0];
        server.sin_addr.S_un.S_un_b.s_b2 = hp->h_addr_list[0][1];
        server.sin_addr.S_un.S_un_b.s_b3 = hp->h_addr_list[0][2];
        server.sin_addr.S_un.S_un_b.s_b4 = hp->h_addr_list[0][3];*/
        server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    
        bind(sd, (struct sockaddr *)&server, sizeof(struct sockaddr_in));
    
    }
    
    int ServerSocket::recieveData()
    {
    
        client_length = (int)sizeof(struct sockaddr_in);
        int test = 0;
    
    
    
        //Receive bytes from client
        bytes_received = recvfrom(sd, buffer, 4096, 0, (struct sockaddr *)&client, &client_length);
    
    
    
    
         bytes_received = sendto(sd, (const char *)"hello", strlen("hello"),
                                0, (const struct sockaddr *) &client,
                                   client_length);
    
    
    
    
        char teststring[40];
        sprintf(teststring,"ErrorCode %d", bytes_received);
        MMISimLogger::instance()->log(teststring);
        MMISimLogger::instance()->close();
    
    
        if (bytes_received < 0)
        {
    
        }
        else
        {
            test = bytes_received;
            //MMISimLogger::instance()->log(buffer);
            //MMISimLogger::instance()->close();
            return test;
        }
    }
    

    I know this code is mess but I don't know where I am doing wrong.

    [Fixed code tags ~kshegunov]



  • It seems that readyRead signal will be not be emitted when we send data from other non-QT application.

    I am able to connect to the server (QT application side) when I send data using send() in non-QT application it returns number of byte send correctly but no readyRead signal is emitted.


  • Moderators

    @Ayush-Gupta said in QT app:exec is blocking the code.:

    It seems that readyRead signal will be not be emitted when we send data from other non-QT application.

    No, there is no differentiation between Qt and non Qt UDP-Source. I'm able to receive and send UDP datagrams just fine that way.

    I am able to connect to the server (QT application side) when I send data using send() in non-QT application it returns number of byte send correctly but no readyRead signal is emitted.

    Pls make sure you actually really send data. A 3rd Party listening software could help here.



  • @Ayush-Gupta said in QT app:exec is blocking the code.:

    It seems that readyRead signal will be not be emitted when we send data from other non-QT application.

    As said multiple times, there is no difference in socket operation between a Qt program versus a non-Qt one, nor between blocking or non-blocking. TCP is TCP, UDP is UDP, regardless of who the sender/receiver is.

    Concentrate instead on discovering what you are doing differently/wrong in either side of the communication.



  • @J-Hilk Is it possible for you to share the example code?



  • void MyTcpServer::incomingConnection(qintptr socketDescriptor)
    {
        socket = new QTcpSocket();
    
        ```
    // set the ID
        if(!socket->setSocketDescriptor(socketDescriptor))
        {
            //OnErrorSocket(socket->error());
            return;
        }
        else
        {
            simForm_->logMessage("Server Started");
            socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
            //OnConnected();
        }
    
        // connect socket and signal
        // note - Qt::DirectConnection is used because it's multithreaded
        //        This makes the slot to be invoked immediately, when the signal is emitted.
        connect(socket, SIGNAL(readyRead()), this, SLOT(OnReadyRead()), Qt::DirectConnection);
        connect(socket, SIGNAL(disconnected()), this, SLOT(OnDisconnected()));
    }
    
    
    incomingConnection call is success means  connection is successful.
    But when sending data using below function. ready Read is not emitting.
    
    int ServerSocket::sendData()
    {
       send(sd , "hello" , strlen("hello") , 0 );
    }
    
    ClientSocket::ClientSocket()
    {
        WSAStartup(0x0101, &w) != 0;
        /* Open a datagram socket */
        sd = socket(AF_INET, SOCK_STREAM, 0);
    
        /* Clear out server struct */
        memset((void *)&server, '\0', sizeof(struct sockaddr_in));
    
        server.sin_family = AF_INET;
        server.sin_port = htons(5000);
        server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    
        connect(sd, (struct sockaddr *)&server, sizeof(server));
    }


  • @J-Hilk @JonB Is I am doing anything wrong in code?


  • Moderators

    @Ayush-Gupta said in QT app:exec is blocking the code.:

    @J-Hilk Is it possible for you to share the example code?

    maybe I would have to cut out as significant part, to be able to show it

    2nd post

    Here you're using a TcpSocket, previously you posted Udp. What is it, do you use both at the same time?

    @J-Hilk @JonB Is I am doing anything wrong in code?

    are you sure you pass the correct port when creating the class. I noticed, your write function has hardcoded port value while your bind uses a value defined at run time



  • @J-Hilk Yes I am using same port number. I failed using UDP then I am trying using UDP.

    Port number is used correctly that is why connection was established and incomingConnection() was called.



  • @J-Hilk Is it possible for you to post some working code?


Log in to reply