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. QT app:exec is blocking the code.
Forum Updated to NodeBB v4.3 + New Features

QT app:exec is blocking the code.

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 4 Posters 2.2k 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.
  • A Ayush Gupta

    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.

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by JonB
    #8

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

    1 Reply Last reply
    1
    • A Offline
      A Offline
      Ayush Gupta
      wrote on last edited by
      #9

      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.

      JonBJ 1 Reply Last reply
      0
      • A Ayush Gupta

        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.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #10

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

        1 Reply Last reply
        1
        • A Offline
          A Offline
          Ayush Gupta
          wrote on last edited by kshegunov
          #11

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Ayush Gupta
            wrote on last edited by
            #12

            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.

            J.HilkJ JonBJ 2 Replies Last reply
            0
            • A Ayush Gupta

              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.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #13

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


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              2
              • A Ayush Gupta

                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.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #14

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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Ayush Gupta
                  wrote on last edited by
                  #15

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

                  J.HilkJ 1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Ayush Gupta
                    wrote on last edited by Ayush Gupta
                    #16
                    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));
                    }
                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Ayush Gupta
                      wrote on last edited by
                      #17

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

                      1 Reply Last reply
                      0
                      • A Ayush Gupta

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

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #18

                        @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


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        1
                        • A Offline
                          A Offline
                          Ayush Gupta
                          wrote on last edited by
                          #19

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

                          A 1 Reply Last reply
                          0
                          • A Ayush Gupta

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

                            A Offline
                            A Offline
                            Ayush Gupta
                            wrote on last edited by
                            #20

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

                            1 Reply Last reply
                            -1

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved