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. Not able to send data via tcp client from thread
Forum Updated to NodeBB v4.3 + New Features

Not able to send data via tcp client from thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 729 Views 2 Watching
  • 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.
  • J JonB
    9 Aug 2024, 14:20

    Shouldn't you wait till you have received QAbstractSocket::connected() before starting to read/write? You don't even know whether it has connected for sure.
    You should put some error handling in code if you are having problems, e.g. on reads and writes.
    I trust your QThread TCPThread; stays in scope.
    Your code does not allow the Qt event loop to run (in the thread). I don't know how that plays with the socket connection/read/write code. It may be fine, I just don't know.

    R Offline
    R Offline
    rohan136
    wrote on 10 Aug 2024, 07:52 last edited by
    #6

    @JonB

    ConnectToHost API works as I get the log messages in the server that the connection is established.

    J 1 Reply Last reply 10 Aug 2024, 08:03
    0
    • R rohan136
      10 Aug 2024, 07:52

      @JonB

      ConnectToHost API works as I get the log messages in the server that the connection is established.

      J Offline
      J Offline
      JonB
      wrote on 10 Aug 2024, 08:03 last edited by JonB 8 Oct 2024, 08:11
      #7

      @rohan136
      I don't see why you would not check that is reflected in the client no matter what happens at the server side. Theoretically at least it might connect at the server side but fail to recognise this or go wrong at the client side.

      Do you not think your client side code should wait for the connection established/finished signal before it starts reading/writing the socket? Even if you know you see the connection established at the server side you do not know when that is seen at the client side. connectToHost() is asynchronous, the socket is not necessarily connected when you execute the next line in the caller. And I don't know how read() behaves on an as-yet-not-connected socket.

      In the code you show so far at least as @Christian-Ehrlicher says there is no need to use any separate thread. You may need one for some purposes, but "the majority" of cases like this we see, at least from new Qt users, have no need of threads.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rohan136
        wrote on 27 Aug 2024, 10:15 last edited by
        #8

        Hi,

        I followed your advice and used signal and slots method for tcp without thread. But right now, I'm facing another issue.
        I've created a C++ class in qml project. And on button click, I'm calling the below function:

        void TCP_Backend::startTcpNetwork()
        {

        TCP_Backend TCP_Class;
        
        
        MySocket = new QTcpSocket;
        
        QObject::connect(MySocket, &QTcpSocket::connected, this, &TCP_Backend::IncomingConnection);
        QObject::connect(MySocket, &QTcpSocket::readyRead, this, &TCP_Backend::ReadMessage);
        QObject::connect(MySocket, &QTcpSocket::disconnected,this, &TCP_Backend::OnDisconnection);
        QObject::connect(this, &TCP_Backend::StartScreenMirroring, this, &TCP_Backend::SendScreenShot);
        QObject::connect(this, &TCP_Backend::ListenToClient, this, &TCP_Backend::WaitForConnection);
        WaitForConnection();
        
         qInfo() << "Socket state" << MySocket->state();
        

        }

        I don't know why but receiver functions in the connect API are not called. When I connect my client with the server, IncomingConnection function should be called, but it's not being called. Also, when I'm checking directly with MySocket->state(), the return value of this function is ConnectedState. Can someone help me why receiver functions are not being called?

        Note: startTcpNetwork is present in TCP_Backend Class.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 27 Aug 2024, 12:20 last edited by
          #9

          You create a socket but don't connect to an endpoint - so what should happen?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • R Offline
            R Offline
            rohan136
            wrote on 28 Aug 2024, 05:56 last edited by
            #10

            What do you mean don't connect to an endpoint? WaitForConnection() has connectToHost and waitForConnected. I mentioned earlier the issue is not for communication, MySocket->state() returns ConnectedState. IncomingConnection API should be triggered by connected signal which is not happening.

            J 1 Reply Last reply 28 Aug 2024, 08:11
            0
            • S Offline
              S Offline
              sbela
              wrote on 28 Aug 2024, 06:52 last edited by
              #11

              I think should look at this example first: https://doc.qt.io/qt-6/qtnetwork-fortuneclient-example.html

              I would like!

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 28 Aug 2024, 07:49 last edited by
                #12

                Please provide all relevant code, not just some parts. Provide a minimal compileable example.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • R rohan136
                  28 Aug 2024, 05:56

                  What do you mean don't connect to an endpoint? WaitForConnection() has connectToHost and waitForConnected. I mentioned earlier the issue is not for communication, MySocket->state() returns ConnectedState. IncomingConnection API should be triggered by connected signal which is not happening.

                  J Offline
                  J Offline
                  JonB
                  wrote on 28 Aug 2024, 08:11 last edited by
                  #13

                  @rohan136
                  We don't know what your WaitForConnection() code might or might not do.

                  Are we to guess MySocket is a member variable in TCP_Backend?

                  Why does your code have a local variable of TCP_Backend TCP_Class; and what does your code do with it? Why would a member method TCP_Backend::startTcpNetwork() create an instance of TCP_Backend under any circumstances? What is the lifetime of whatever TCP_Backend instance that method TCP_Backend::startTcpNetwork() is being called on?

                  This is why you need to provide some "minimal compileable example".

                  1 Reply Last reply
                  1
                  • R Offline
                    R Offline
                    rohan136
                    wrote on 29 Aug 2024, 07:19 last edited by
                    #14

                    Hi Jon,

                    Thanks for the reply, I figured our was creating socket twice in my code that's why it was working properly. Now my issue is resolved.

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 29 Aug 2024, 10:35 last edited by
                      #15

                      Then please mark the topic as solved.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      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