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. I have problem with sender() in QTcpSocket::disconnected signal
Forum Updated to NodeBB v4.3 + New Features

I have problem with sender() in QTcpSocket::disconnected signal

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 1.5k Views 1 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.
  • Kirill GusarevK Kirill Gusarev

    Hello. I make server and client apps in C++ Qt.
    In server side I store sockets of connected clients in QVector<QTcpSocket*>.
    I want to remove socket from QVector when I close a client's window. But in function for signal QTcpSocket::disconnected the sender() returns nullptr and I can't get any information of this lost socket.
    Help pls :C
    Sorry for mistakes.

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #2

    @Kirill-Gusarev
    If that is so you will need to use lambdas, to which you can pass a parameter of which socket is closing, instead of sender(), which you should avoid if possible.

    Kirill GusarevK 1 Reply Last reply
    2
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #3

      Hi and welcome to devnet,

      Can you share the code where you handle the new sockets and their disconnection handling ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Kirill GusarevK 1 Reply Last reply
      1
      • JonBJ JonB

        @Kirill-Gusarev
        If that is so you will need to use lambdas, to which you can pass a parameter of which socket is closing, instead of sender(), which you should avoid if possible.

        Kirill GusarevK Offline
        Kirill GusarevK Offline
        Kirill Gusarev
        wrote on last edited by
        #4

        @JonB thanks for answer. Can you show me example of this lambda? (I'm just not sure that I understand you correctly)

        Ъуъ

        JonBJ 1 Reply Last reply
        0
        • Kirill GusarevK Kirill Gusarev

          @JonB thanks for answer. Can you show me example of this lambda? (I'm just not sure that I understand you correctly)

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #5

          @Kirill-Gusarev
          Actually I don't know why you need to know about the sender here anyway, and hence might have wanted a lambda? Don't you just want to remove this from your QVector<QTcpSocket*> in disconnected slot? @SGaist asked

          Can you share the code where you handle the new sockets and their disconnection handling ?

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi and welcome to devnet,

            Can you share the code where you handle the new sockets and their disconnection handling ?

            Kirill GusarevK Offline
            Kirill GusarevK Offline
            Kirill Gusarev
            wrote on last edited by
            #6

            @SGaist Hi!

            Incoming connection:

            void Server::incomingConnection(qintptr socketDescriptor)
            {
                socket = new QTcpSocket();
                socket->setSocketDescriptor(socketDescriptor);
                connect(socket, &QTcpSocket::readyRead, this, &Server::slotReadyRead);
                // connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
                /*
                connect(socket, &QTcpSocket::disconnected, socket, [=]()
                {
                    qDebug() << "Number of sockets:" << sockets.size();
                    qDebug() << "SOCKET DISCONNECTED";
            //        qDebug() << "Sockets removed:" << erase(sockets, sender());
                    qDebug() << "Sockets removed:" << sockets.removeOne(sender());
                    qDebug() << "Number of sockets:" << sockets.size();
            //        qDebug() << sender()->objectName(); // Краш
            //        qDebug() << ((QTcpSocket*)(sender()))->socketDescriptor(); // Краш
                    qDebug() << qobject_cast<QTcpSocket*>(sender())->socketDescriptor();
                });
                */
                connect(socket, &QTcpSocket::disconnected, socket, [=]()
                {
                    QTcpSocket* client = qobject_cast<QTcpSocket*>(sender());
                    if (client)
                    {
                        qDebug() << "if";
                        qDebug() << client;
                    }
                    else
                    {
                        qDebug() << "else";
                        qDebug() << client->socketDescriptor(); // crash (Segmentation fault)
                    }
                });
            
                sockets.push_back(socket);
                qDebug() << "client connected:" << socketDescriptor;
            }
            

            Function for "disconnected" signal:

            connect(socket, &QTcpSocket::disconnected, socket, [=]()
                {
                    QTcpSocket* client = qobject_cast<QTcpSocket*>(sender());
                    if (client)
                    {
                        qDebug() << "if";
                        qDebug() << client;
                    }
                    else
                    {
                        qDebug() << "else";
                        qDebug() << client->socketDescriptor(); // crash (Segmentation fault)
                    }
                });
            

            Ъуъ

            JonBJ 1 Reply Last reply
            0
            • Kirill GusarevK Kirill Gusarev

              @SGaist Hi!

              Incoming connection:

              void Server::incomingConnection(qintptr socketDescriptor)
              {
                  socket = new QTcpSocket();
                  socket->setSocketDescriptor(socketDescriptor);
                  connect(socket, &QTcpSocket::readyRead, this, &Server::slotReadyRead);
                  // connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
                  /*
                  connect(socket, &QTcpSocket::disconnected, socket, [=]()
                  {
                      qDebug() << "Number of sockets:" << sockets.size();
                      qDebug() << "SOCKET DISCONNECTED";
              //        qDebug() << "Sockets removed:" << erase(sockets, sender());
                      qDebug() << "Sockets removed:" << sockets.removeOne(sender());
                      qDebug() << "Number of sockets:" << sockets.size();
              //        qDebug() << sender()->objectName(); // Краш
              //        qDebug() << ((QTcpSocket*)(sender()))->socketDescriptor(); // Краш
                      qDebug() << qobject_cast<QTcpSocket*>(sender())->socketDescriptor();
                  });
                  */
                  connect(socket, &QTcpSocket::disconnected, socket, [=]()
                  {
                      QTcpSocket* client = qobject_cast<QTcpSocket*>(sender());
                      if (client)
                      {
                          qDebug() << "if";
                          qDebug() << client;
                      }
                      else
                      {
                          qDebug() << "else";
                          qDebug() << client->socketDescriptor(); // crash (Segmentation fault)
                      }
                  });
              
                  sockets.push_back(socket);
                  qDebug() << "client connected:" << socketDescriptor;
              }
              

              Function for "disconnected" signal:

              connect(socket, &QTcpSocket::disconnected, socket, [=]()
                  {
                      QTcpSocket* client = qobject_cast<QTcpSocket*>(sender());
                      if (client)
                      {
                          qDebug() << "if";
                          qDebug() << client;
                      }
                      else
                      {
                          qDebug() << "else";
                          qDebug() << client->socketDescriptor(); // crash (Segmentation fault)
                      }
                  });
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #7

              @Kirill-Gusarev
              So you already have a lambda anyway.
              Why are you looking at sender() and not just using this?
              Oh, I get it, this is server, you expect sender() to be client?
              And even when it works, why would you want to access a socket's socketDescriptor() during disconnect? I don't know whether it's valid at that point.

              sockets.push_back(socket);

              So don't you just want to remove socket or this from sockets during disconnected? I'm not understanding why you need sender() (nor socketDescriptor()).

              Kirill GusarevK 1 Reply Last reply
              0
              • JonBJ JonB

                @Kirill-Gusarev
                So you already have a lambda anyway.
                Why are you looking at sender() and not just using this?
                Oh, I get it, this is server, you expect sender() to be client?
                And even when it works, why would you want to access a socket's socketDescriptor() during disconnect? I don't know whether it's valid at that point.

                sockets.push_back(socket);

                So don't you just want to remove socket or this from sockets during disconnected? I'm not understanding why you need sender() (nor socketDescriptor()).

                Kirill GusarevK Offline
                Kirill GusarevK Offline
                Kirill Gusarev
                wrote on last edited by
                #8

                @JonB sorry i have a 10 minute messaging limit because i am a new user.

                Yes, it is a Server-side code in Server:QTcpServer class.

                Calling of ->socketDescriptor is a just example of crashing while trying to get any information from sender().

                I just want to delete this socket from QVector. I thought i can take socket information from sender() :((, but I take only empty pointer(((

                Ъуъ

                JonBJ 1 Reply Last reply
                0
                • Kirill GusarevK Kirill Gusarev

                  @JonB sorry i have a 10 minute messaging limit because i am a new user.

                  Yes, it is a Server-side code in Server:QTcpServer class.

                  Calling of ->socketDescriptor is a just example of crashing while trying to get any information from sender().

                  I just want to delete this socket from QVector. I thought i can take socket information from sender() :((, but I take only empty pointer(((

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #9

                  @Kirill-Gusarev
                  I have now answered a couple of times:

                  So don't you just want to remove socket or this from sockets during disconnected? I'm not understanding why you need sender()

                  Kirill GusarevK 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Kirill-Gusarev
                    I have now answered a couple of times:

                    So don't you just want to remove socket or this from sockets during disconnected? I'm not understanding why you need sender()

                    Kirill GusarevK Offline
                    Kirill GusarevK Offline
                    Kirill Gusarev
                    wrote on last edited by Kirill Gusarev
                    #10

                    @JonB said in I have problem with sender() in QTcpSocket::disconnected signal:

                    @Kirill-Gusarev
                    If that is so you will need to use lambdas, to which you can pass a parameter of which socket is closing, instead of sender(), which you should avoid if possible.

                    [socket](){}
                    

                    [socket] - is it "pass a parameter of which socket is closing"?

                    Ъуъ

                    JonBJ 1 Reply Last reply
                    1
                    • Kirill GusarevK Kirill Gusarev

                      @JonB said in I have problem with sender() in QTcpSocket::disconnected signal:

                      @Kirill-Gusarev
                      If that is so you will need to use lambdas, to which you can pass a parameter of which socket is closing, instead of sender(), which you should avoid if possible.

                      [socket](){}
                      

                      [socket] - is it "pass a parameter of which socket is closing"?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #11

                      @Kirill-Gusarev
                      Indeed! Specifically it is whatever value socket had at the time that connect() statement was executed. Which was immediately prior to sockets.push_back(socket);.

                      Or it would have been this if you had derived from QTcpSocket and just provided a slot to attach to disconnected. But with lambda you can pass it whatever variables you like without having to subclass.

                      Kirill GusarevK 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @Kirill-Gusarev
                        Indeed! Specifically it is whatever value socket had at the time that connect() statement was executed. Which was immediately prior to sockets.push_back(socket);.

                        Or it would have been this if you had derived from QTcpSocket and just provided a slot to attach to disconnected. But with lambda you can pass it whatever variables you like without having to subclass.

                        Kirill GusarevK Offline
                        Kirill GusarevK Offline
                        Kirill Gusarev
                        wrote on last edited by Kirill Gusarev
                        #12

                        @JonB thank you. Sorry for my mistakes and missunderstandings, my main language is russian.
                        [socket] - that helped. I solved the problem :]

                        I have updated code (added QTcpSocket *currentSocket):

                        void Server::incomingConnection(qintptr socketDescriptor)
                        {
                            socket = new QTcpSocket();
                            socket->setSocketDescriptor(socketDescriptor);
                            connect(socket, &QTcpSocket::readyRead, this, &Server::slotReadyRead);
                            QTcpSocket *currentSocket = socket;
                            connect(socket, &QTcpSocket::disconnected, socket, [=]()
                            {
                                qDebug() << currentSocket << "socket removed:" << sockets.removeOne(currentSocket);
                            });
                        
                            sockets.push_back(socket);
                            qDebug() << "client connected" << socketDescriptor;
                        }
                        

                        Ъуъ

                        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