Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. can't receive data via TcpSocket but can send!
QtWS25 Last Chance

can't receive data via TcpSocket but can send!

Scheduled Pinned Locked Moved Solved Mobile and Embedded
12 Posts 3 Posters 1.3k 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 ali1377
    11 Sept 2019, 20:55

    Hi All
    I have a little problem with QTcpSocket.
    I made a socket and I connect to a server which has made by-- tcp server --pc software .
    I send data to server but I cant receive data !

    A Offline
    A Offline
    ali1377
    wrote on 11 Sept 2019, 21:10 last edited by
    #2

    @ali1377
    connect(tcpSocket, SIGNAL(connected()),this, SLOT(tcpConnected()));
    connect(tcpSocket, SIGNAL(disconnected()),this, SLOT(tcpDisconnected()));
    connect(tcpSocket, SIGNAL(bytesWritten(qint64)),this, SLOT(tcpBytesWritten(qint64)));
    connect(this, SIGNAL(newTCPData(QStringList)), this, SLOT(Draw_new_set_of_Data(QByteArray*)));
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readTCPData()));
    connect(tcpSocket,QOverloadQAbstractSocket::SocketError::of(&QAbstractSocket::error),
    this, &My_drawing_object::displaySocketTCPConnectionError);

    ///Try Connection To Device
    tcpSocket->abort();
    tcpSocket->connectToHost("192.168.1.110",48569);
    

    I defined readTcpData but when I click on send button in Tcp Client Server(pc software for making tcp server) I cant receive data.but my sended data received at software correctly.but why?

    A 1 Reply Last reply 12 Sept 2019, 05:03
    0
    • A ali1377
      11 Sept 2019, 21:10

      @ali1377
      connect(tcpSocket, SIGNAL(connected()),this, SLOT(tcpConnected()));
      connect(tcpSocket, SIGNAL(disconnected()),this, SLOT(tcpDisconnected()));
      connect(tcpSocket, SIGNAL(bytesWritten(qint64)),this, SLOT(tcpBytesWritten(qint64)));
      connect(this, SIGNAL(newTCPData(QStringList)), this, SLOT(Draw_new_set_of_Data(QByteArray*)));
      connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readTCPData()));
      connect(tcpSocket,QOverloadQAbstractSocket::SocketError::of(&QAbstractSocket::error),
      this, &My_drawing_object::displaySocketTCPConnectionError);

      ///Try Connection To Device
      tcpSocket->abort();
      tcpSocket->connectToHost("192.168.1.110",48569);
      

      I defined readTcpData but when I click on send button in Tcp Client Server(pc software for making tcp server) I cant receive data.but my sended data received at software correctly.but why?

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 12 Sept 2019, 05:03 last edited by
      #3

      Hi @ali1377

      is readTCPData() a slot?

      Also, you don't check the return value of connect. Better convert the connects to the new style functor connects, so the compiler checks for you.

      Regards

      Qt has to stay free or it will die.

      A 1 Reply Last reply 12 Sept 2019, 07:54
      2
      • A aha_1980
        12 Sept 2019, 05:03

        Hi @ali1377

        is readTCPData() a slot?

        Also, you don't check the return value of connect. Better convert the connects to the new style functor connects, so the compiler checks for you.

        Regards

        A Offline
        A Offline
        ali1377
        wrote on 12 Sept 2019, 07:54 last edited by ali1377 9 Dec 2019, 07:55
        #4

        @aha_1980
        hi @aha_1980
        yes That is a slot.
        I changed connect style to
        QObject::connect(
        tcpSocket, &QTcpSocket::readyRead,
        = { qDebug() << "GOT DATA " << tcpSocket->readAll(); }
        );
        but it didn't help me. Still I can't receive data.
        you can see I could send my data to server but can't send to my app from server.(when I click on send button in software has shown below)
        ![0_1568274672557_Capture.PNG] (https://ddgobkiprc33d.cloudfront.net/1e705b96-0c05-4b6e-87a1-50f708dd24ae.PNG)

        A 1 Reply Last reply 12 Sept 2019, 08:12
        0
        • A ali1377
          12 Sept 2019, 07:54

          @aha_1980
          hi @aha_1980
          yes That is a slot.
          I changed connect style to
          QObject::connect(
          tcpSocket, &QTcpSocket::readyRead,
          = { qDebug() << "GOT DATA " << tcpSocket->readAll(); }
          );
          but it didn't help me. Still I can't receive data.
          you can see I could send my data to server but can't send to my app from server.(when I click on send button in software has shown below)
          ![0_1568274672557_Capture.PNG] (https://ddgobkiprc33d.cloudfront.net/1e705b96-0c05-4b6e-87a1-50f708dd24ae.PNG)

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 12 Sept 2019, 08:12 last edited by
          #5

          @ali1377

          • But is the receive slot called at all?
          • Where is tcpSocket defined?
          • Can you show where you create tcpSocket?
          • Why do you call tcpSocket->abort();?

          Qt has to stay free or it will die.

          A 1 Reply Last reply 12 Sept 2019, 08:31
          0
          • A aha_1980
            12 Sept 2019, 08:12

            @ali1377

            • But is the receive slot called at all?
            • Where is tcpSocket defined?
            • Can you show where you create tcpSocket?
            • Why do you call tcpSocket->abort();?
            A Offline
            A Offline
            ali1377
            wrote on 12 Sept 2019, 08:31 last edited by
            #6

            @aha_1980
            actually this code is not mine.
            but I can show you any part of this code .I can't uplode whole code because its it's more than 9000 line.
            I have defined in constructor .

            My_drawing_object::My_drawing_object(QObject *parent) : QObject(parent), tcpSocket(new QTcpSocket(this)){

            tcpSocket = new QTcpSocket(this);

            //connect(timer, SIGNAL(timeout()), this, SLOT(:Draw_new_set_of_Data(QByteArray* dataRecieved)),static_cast<Qt::ConnectionType>(Qt::UniqueConnection));
            
            
            requestNewTCPConnection();
            //connect(tcpSocket, &QIODevice::readyRead, this, &My_drawing_object::readDataOverTCPConnection);
            
            QNetworkConfigurationManager manager;
             if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
                 // Get saved network configuration
                 QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
                 settings.beginGroup(QLatin1String("QtNetwork"));
                 const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
                 settings.endGroup();
            
                 // If the saved network configuration is not currently discovered use the system default
                 QNetworkConfiguration config = manager.configurationFromIdentifier(id);
                 if ((config.state() & QNetworkConfiguration::Discovered) !=
                     QNetworkConfiguration::Discovered) {
                     config = manager.defaultConfiguration();
                 }
            
                 networkSession = new QNetworkSession(config, this);
                 connect(networkSession, &QNetworkSession::opened, this, &My_drawing_object::TCPConnectionSessionOpened);
            
                 //getFortuneButton->setEnabled(false);
                 //statusLabel->setText(tr("Opening network session."));
                 networkSession->open();
             }
            

            }

            I don't know usage of abort .
            I think when I send a packet to app it must show qdebug Line has defined in connect part.

            A 1 Reply Last reply 12 Sept 2019, 08:53
            0
            • A ali1377
              12 Sept 2019, 08:31

              @aha_1980
              actually this code is not mine.
              but I can show you any part of this code .I can't uplode whole code because its it's more than 9000 line.
              I have defined in constructor .

              My_drawing_object::My_drawing_object(QObject *parent) : QObject(parent), tcpSocket(new QTcpSocket(this)){

              tcpSocket = new QTcpSocket(this);

              //connect(timer, SIGNAL(timeout()), this, SLOT(:Draw_new_set_of_Data(QByteArray* dataRecieved)),static_cast<Qt::ConnectionType>(Qt::UniqueConnection));
              
              
              requestNewTCPConnection();
              //connect(tcpSocket, &QIODevice::readyRead, this, &My_drawing_object::readDataOverTCPConnection);
              
              QNetworkConfigurationManager manager;
               if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
                   // Get saved network configuration
                   QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
                   settings.beginGroup(QLatin1String("QtNetwork"));
                   const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
                   settings.endGroup();
              
                   // If the saved network configuration is not currently discovered use the system default
                   QNetworkConfiguration config = manager.configurationFromIdentifier(id);
                   if ((config.state() & QNetworkConfiguration::Discovered) !=
                       QNetworkConfiguration::Discovered) {
                       config = manager.defaultConfiguration();
                   }
              
                   networkSession = new QNetworkSession(config, this);
                   connect(networkSession, &QNetworkSession::opened, this, &My_drawing_object::TCPConnectionSessionOpened);
              
                   //getFortuneButton->setEnabled(false);
                   //statusLabel->setText(tr("Opening network session."));
                   networkSession->open();
               }
              

              }

              I don't know usage of abort .
              I think when I send a packet to app it must show qdebug Line has defined in connect part.

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 12 Sept 2019, 08:53 last edited by
              #7

              @ali1377 said in can't receive data via TcpSocket but can send!:

              My_drawing_object::My_drawing_object(QObject *parent) : QObject(parent), tcpSocket(new QTcpSocket(this)){
              tcpSocket = new QTcpSocket(this);

              Do you see this? You already create the QTcpSocket twice, discarding the first object (memory leak!).

              You should make sure that tcpSocket is not destroyed somewhere in your program (e.g. because My_drawing_object is destroyed).

              Also two suggestions:

              • Name member variables with leading m_ (e.g. m_tcpSocket), that makes it easier for reviewers
              • For the class name I'd suggest MyDrawingObject

              Qt has to stay free or it will die.

              A 1 Reply Last reply 12 Sept 2019, 09:33
              2
              • A aha_1980
                12 Sept 2019, 08:53

                @ali1377 said in can't receive data via TcpSocket but can send!:

                My_drawing_object::My_drawing_object(QObject *parent) : QObject(parent), tcpSocket(new QTcpSocket(this)){
                tcpSocket = new QTcpSocket(this);

                Do you see this? You already create the QTcpSocket twice, discarding the first object (memory leak!).

                You should make sure that tcpSocket is not destroyed somewhere in your program (e.g. because My_drawing_object is destroyed).

                Also two suggestions:

                • Name member variables with leading m_ (e.g. m_tcpSocket), that makes it easier for reviewers
                • For the class name I'd suggest MyDrawingObject
                A Offline
                A Offline
                ali1377
                wrote on 12 Sept 2019, 09:33 last edited by
                #8

                @aha_1980
                thanks for suggestions .I corrected second define of tcpSocket.
                During the run I can send data to server and it shows the socket exists and didn't destroy.
                so ..... .

                A 1 Reply Last reply 12 Sept 2019, 10:43
                0
                • A ali1377
                  12 Sept 2019, 09:33

                  @aha_1980
                  thanks for suggestions .I corrected second define of tcpSocket.
                  During the run I can send data to server and it shows the socket exists and didn't destroy.
                  so ..... .

                  A Offline
                  A Offline
                  ali1377
                  wrote on 12 Sept 2019, 10:43 last edited by
                  #9

                  @ali1377
                  I tried with PacketSender too. but it showed "couldn't connect".
                  where is problem.?
                  I sent to IP ,PORT which app connected with them to server made by TCP client Server software
                  0_1568284772974_Capture.PNG

                  J 1 Reply Last reply 12 Sept 2019, 14:38
                  0
                  • A ali1377
                    12 Sept 2019, 10:43

                    @ali1377
                    I tried with PacketSender too. but it showed "couldn't connect".
                    where is problem.?
                    I sent to IP ,PORT which app connected with them to server made by TCP client Server software
                    0_1568284772974_Capture.PNG

                    J Offline
                    J Offline
                    JonB
                    wrote on 12 Sept 2019, 14:38 last edited by JonB 9 Dec 2019, 14:49
                    #10

                    @ali1377

                    I tried with PacketSender too. but it showed "couldn't connect".
                    I sent to IP ,PORT which app connected with them to server made by TCP client Server software

                    • You show a screenshot with "Could not connect" on port 41025.

                    • In the same shot a different piece of software is looking at port 48569, and that is what your code uses.

                    ?

                    What does your code slot do for disconnected and/or error? Make sure you are sure they are not being hit?

                    qDebug() << "GOT DATA " << tcpSocket->readAll();

                    The incoming data is not text characters. Do you mean you never see the GOT DATA or you just do not see anything to the right of it?

                    A 1 Reply Last reply 12 Sept 2019, 18:11
                    1
                    • J JonB
                      12 Sept 2019, 14:38

                      @ali1377

                      I tried with PacketSender too. but it showed "couldn't connect".
                      I sent to IP ,PORT which app connected with them to server made by TCP client Server software

                      • You show a screenshot with "Could not connect" on port 41025.

                      • In the same shot a different piece of software is looking at port 48569, and that is what your code uses.

                      ?

                      What does your code slot do for disconnected and/or error? Make sure you are sure they are not being hit?

                      qDebug() << "GOT DATA " << tcpSocket->readAll();

                      The incoming data is not text characters. Do you mean you never see the GOT DATA or you just do not see anything to the right of it?

                      A Offline
                      A Offline
                      ali1377
                      wrote on 12 Sept 2019, 18:11 last edited by
                      #11

                      @jonb
                      thanks for your reply.The problem was changing the port.I sent my packets to server which has made by TcpClientServer .The software shows ip and port .but I must notice that this port is not bound to socket and it can change .so now I bound a port like 4000 to socket and now I can receive packets.

                      A 1 Reply Last reply 12 Sept 2019, 18:17
                      0
                      • A ali1377
                        12 Sept 2019, 18:11

                        @jonb
                        thanks for your reply.The problem was changing the port.I sent my packets to server which has made by TcpClientServer .The software shows ip and port .but I must notice that this port is not bound to socket and it can change .so now I bound a port like 4000 to socket and now I can receive packets.

                        A Offline
                        A Offline
                        ali1377
                        wrote on 12 Sept 2019, 18:17 last edited by
                        #12

                        @ali1377
                        In my idea maybe with connecting to a server made by my code with nextPendingConnection function we don't loose port but with this software it change. That is my idea and maybe it's no true.

                        1 Reply Last reply
                        0

                        11/12

                        12 Sept 2019, 18:11

                        • Login

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