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. TCP in Qt don't send data
Forum Updated to NodeBB v4.3 + New Features

TCP in Qt don't send data

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 796 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 Offline
    A Offline
    Athena_S
    wrote on 13 May 2022, 12:56 last edited by
    #1

    Hello, good afternoon,
    I have a problem with Qt. I have created a class that sends data through a TCP socket without depending on the Qt part. I have tested in a test that the data is sent every 1Hz without closing the communication and it has worked correctly. The problem is the following:
    When I add this class to Qt and create a thread to send data every 1Hz. The server receives all the accumulated data when the socket is closed or the application is closed.
    Do I have to configure something in qt to allow TCP data sending?

    Also, I have tried Qtcpsocket and it behaves the same.

    Thank you so much for your help.

    J J 2 Replies Last reply 13 May 2022, 12:58
    0
    • A Athena_S
      13 May 2022, 12:56

      Hello, good afternoon,
      I have a problem with Qt. I have created a class that sends data through a TCP socket without depending on the Qt part. I have tested in a test that the data is sent every 1Hz without closing the communication and it has worked correctly. The problem is the following:
      When I add this class to Qt and create a thread to send data every 1Hz. The server receives all the accumulated data when the socket is closed or the application is closed.
      Do I have to configure something in qt to allow TCP data sending?

      Also, I have tried Qtcpsocket and it behaves the same.

      Thank you so much for your help.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 13 May 2022, 12:58 last edited by
      #2

      @Athena_S Please post your code, else only guessing is possible...

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

      1 Reply Last reply
      1
      • A Athena_S
        13 May 2022, 12:56

        Hello, good afternoon,
        I have a problem with Qt. I have created a class that sends data through a TCP socket without depending on the Qt part. I have tested in a test that the data is sent every 1Hz without closing the communication and it has worked correctly. The problem is the following:
        When I add this class to Qt and create a thread to send data every 1Hz. The server receives all the accumulated data when the socket is closed or the application is closed.
        Do I have to configure something in qt to allow TCP data sending?

        Also, I have tried Qtcpsocket and it behaves the same.

        Thank you so much for your help.

        J Offline
        J Offline
        JonB
        wrote on 13 May 2022, 12:59 last edited by
        #3

        @Athena_S said in TCP in Qt don't send data:

        Do I have to configure something in qt to allow TCP data sending?

        Nope it should all work. Yes you should use QTcpSocket. Your code is probably not doing something correctly, we need to see it (or preferably a minimal example of an issue) to comment further....

        J 1 Reply Last reply 13 May 2022, 13:01
        1
        • J JonB
          13 May 2022, 12:59

          @Athena_S said in TCP in Qt don't send data:

          Do I have to configure something in qt to allow TCP data sending?

          Nope it should all work. Yes you should use QTcpSocket. Your code is probably not doing something correctly, we need to see it (or preferably a minimal example of an issue) to comment further....

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 13 May 2022, 13:01 last edited by
          #4

          @JonB said in TCP in Qt don't send data:

          Your code is probably not doing something correctly

          alt text


          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
          3
          • A Offline
            A Offline
            Athena_S
            wrote on 17 May 2022, 06:23 last edited by Athena_S
            #5
            Constructor(std::string ip, uint32_t port, Socket::protocol protocol_type)
            {
            
              // This is a Socket that i created
              /*socket_server_= std::make_unique<Socket>(ip, port, protocol_type);
              socket_server_->init(false);*/
            
              host_address_ = QHostAddress(QString::fromStdString(ip));
              tcp_client_socket_.reset(new QTcpSocket());
              tcp_client_socket_->connectToHost(QString::fromStdString(ip), port);
            
            }
            
            
            void sendDataFunction()
            {
              iris_data_.updateTime();
            
              size_t size = iris_data_.telemetryData.getSumAllBytes();
              char* command = new char[size];
              uint8_t* msg = iris_data_.telemetryData.getMsg();
              memcpy(command, msg, size_t(iris_data_.telemetryData.getSumAllBytes()));
            
              tcp_client_socket_->write(command,size);
              tcp_client_socket_->waitForBytesWritten(1000);
              delete[] command;
            }
            
            

            I create this class, with his constructor and send data member function.
            When the instance of the class is created, a qtimer call sendDataFunction every second.

            To read the data a use a TCP Server in python.

            import socket
            import sys
            
            # Create a TCP/IP socket
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            
            # Bind the socket to the port
            server_address = ('localhost', 10000)
            print('starting up on {} port {}'.format(*server_address))
            sock.bind(server_address)
            
            # Listen for incoming connections
            sock.listen(1)
            
            while True:
                # Wait for a connection
                print('waiting for a connection')
                connection, client_address = sock.accept()
                try:
                    print('connection from', client_address)
            
                    # Receive the data in small chunks and retransmit it
                    while True:
                        data = connection.recv(241)
                        print('received {}'.format(data))
                        if data:
                            print('sending data back to the client')
                            #connection.sendall(data)
                        else:
                            print('no data from', client_address)
                            break
            
                finally:
                    # Clean up the connection
                    connection.close()
            
            

            QTIMER:

            timer_pcs_iris_.reset(new QTimer(this));
            timer_pcs_iris_.get()->setInterval(1000);
            connect(timer_pcs_iris_.get(),
                      SIGNAL(timeout()),
                      this,
                      SLOT(sendData()));
            timer_pcs_iris_.get()->start();
            
            void sendData()
            {
            //Instance of the classs
              pcs_iris_->sendDataFunction();
            }
            
            J 1 Reply Last reply 17 May 2022, 06:27
            0
            • A Athena_S
              17 May 2022, 06:23
              Constructor(std::string ip, uint32_t port, Socket::protocol protocol_type)
              {
              
                // This is a Socket that i created
                /*socket_server_= std::make_unique<Socket>(ip, port, protocol_type);
                socket_server_->init(false);*/
              
                host_address_ = QHostAddress(QString::fromStdString(ip));
                tcp_client_socket_.reset(new QTcpSocket());
                tcp_client_socket_->connectToHost(QString::fromStdString(ip), port);
              
              }
              
              
              void sendDataFunction()
              {
                iris_data_.updateTime();
              
                size_t size = iris_data_.telemetryData.getSumAllBytes();
                char* command = new char[size];
                uint8_t* msg = iris_data_.telemetryData.getMsg();
                memcpy(command, msg, size_t(iris_data_.telemetryData.getSumAllBytes()));
              
                tcp_client_socket_->write(command,size);
                tcp_client_socket_->waitForBytesWritten(1000);
                delete[] command;
              }
              
              

              I create this class, with his constructor and send data member function.
              When the instance of the class is created, a qtimer call sendDataFunction every second.

              To read the data a use a TCP Server in python.

              import socket
              import sys
              
              # Create a TCP/IP socket
              sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
              
              # Bind the socket to the port
              server_address = ('localhost', 10000)
              print('starting up on {} port {}'.format(*server_address))
              sock.bind(server_address)
              
              # Listen for incoming connections
              sock.listen(1)
              
              while True:
                  # Wait for a connection
                  print('waiting for a connection')
                  connection, client_address = sock.accept()
                  try:
                      print('connection from', client_address)
              
                      # Receive the data in small chunks and retransmit it
                      while True:
                          data = connection.recv(241)
                          print('received {}'.format(data))
                          if data:
                              print('sending data back to the client')
                              #connection.sendall(data)
                          else:
                              print('no data from', client_address)
                              break
              
                  finally:
                      # Clean up the connection
                      connection.close()
              
              

              QTIMER:

              timer_pcs_iris_.reset(new QTimer(this));
              timer_pcs_iris_.get()->setInterval(1000);
              connect(timer_pcs_iris_.get(),
                        SIGNAL(timeout()),
                        this,
                        SLOT(sendData()));
              timer_pcs_iris_.get()->start();
              
              void sendData()
              {
              //Instance of the classs
                pcs_iris_->sendDataFunction();
              }
              
              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 17 May 2022, 06:27 last edited by
              #6

              @Athena_S said in TCP in Qt don't send data:

              I create this class, with his constructor and send data member function

              You forgot to post the code where you're actually calling sendDataFunction().
              Also, you do not check whether connectToHost succeeds, basically you do not have any error handling.

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

              A 1 Reply Last reply 17 May 2022, 06:52
              1
              • J jsulm
                17 May 2022, 06:27

                @Athena_S said in TCP in Qt don't send data:

                I create this class, with his constructor and send data member function

                You forgot to post the code where you're actually calling sendDataFunction().
                Also, you do not check whether connectToHost succeeds, basically you do not have any error handling.

                A Offline
                A Offline
                Athena_S
                wrote on 17 May 2022, 06:52 last edited by
                #7

                @jsulm I add the qtimer function.
                You are right, I forget check if the connection succeeds, but the python script show me that one connect to server but i don't receive data

                J 1 Reply Last reply 17 May 2022, 06:58
                0
                • A Athena_S
                  17 May 2022, 06:52

                  @jsulm I add the qtimer function.
                  You are right, I forget check if the connection succeeds, but the python script show me that one connect to server but i don't receive data

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 17 May 2022, 06:58 last edited by
                  #8

                  @Athena_S Did you check whether sendData() is called?
                  And please add error handling!
                  For example: what does tcp_client_socket_->write(command,size) return?
                  Also connect a slot to https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred and print error there.
                  All this is something you should anyway do...

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

                  1 Reply Last reply
                  0

                  1/8

                  13 May 2022, 12:56

                  • Login

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