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. how readyread() know about from which socket ,it is called
Forum Updated to NodeBB v4.3 + New Features

how readyread() know about from which socket ,it is called

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.4k 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.
  • J Offline
    J Offline
    JadeN001
    wrote on last edited by
    #1

    If i have connected multiple client to server,In that case how readyread() signal know which socket called it.
    connect(socket,SIGNAL(readyRead()),this,SLOT(Read()),Qt::DirectConnection);
    if i write qDebug()<<socketDescriptor in Read() SLOT,it gives particular socketDescriptor of the socket connected to it.so my question is how Read() will know about that socketDescriptor value.

    1 Reply Last reply
    0
    • Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      Hi,

      In your "Read()" slot, you can retrieve the QTcpSocket by using sender().

      void MyClass::Read() //Read slot
      {
          QTcpSocket * socket = static_cast<QTcpSocket *>(sender());
          if(!socket)
               return; //Problem to retrieve the socket, ususally should not happen.
      
          QByteArray data = socket->readAll();
          //...
      }
      
      J 2 Replies Last reply
      1
      • Gojir4G Gojir4

        Hi,

        In your "Read()" slot, you can retrieve the QTcpSocket by using sender().

        void MyClass::Read() //Read slot
        {
            QTcpSocket * socket = static_cast<QTcpSocket *>(sender());
            if(!socket)
                 return; //Problem to retrieve the socket, ususally should not happen.
        
            QByteArray data = socket->readAll();
            //...
        }
        
        J Offline
        J Offline
        JadeN001
        wrote on last edited by
        #3

        @Gojir4 thanks. if we dont use sender() syntax.it can also be possible by doing..
        QByteArray data1= socket->readAll();
        In this socket is initialize in header file and the declared it in run() of thread class
        like this:
        socket=new QTcpSocket();

        if(!socket->setSocketDescriptor(this->socketDesc))
        {
            emit error(socket->error());
            return;
        }
        connect(socket,SIGNAL(readyRead()),this,SLOT(Read()),Qt::DirectConnection);
        

        so my question is in code below:
        void MyThread::Read()
        {
        qDebug()<<"socket in Read:"<<s;
        QByteArray data1= socket->readAll();//{qDebug()<<"Message received ";}
        qDebug()<<socketDescriptor<<"Data is here"<<data1;
        }

        how Read function know about particular socketDescriptor of socket if we have multiple client connected .Because in that situation there are more than one socket are connected. (socketDescriptor is initialized in header file)

        JonBJ 1 Reply Last reply
        0
        • J JadeN001

          @Gojir4 thanks. if we dont use sender() syntax.it can also be possible by doing..
          QByteArray data1= socket->readAll();
          In this socket is initialize in header file and the declared it in run() of thread class
          like this:
          socket=new QTcpSocket();

          if(!socket->setSocketDescriptor(this->socketDesc))
          {
              emit error(socket->error());
              return;
          }
          connect(socket,SIGNAL(readyRead()),this,SLOT(Read()),Qt::DirectConnection);
          

          so my question is in code below:
          void MyThread::Read()
          {
          qDebug()<<"socket in Read:"<<s;
          QByteArray data1= socket->readAll();//{qDebug()<<"Message received ";}
          qDebug()<<socketDescriptor<<"Data is here"<<data1;
          }

          how Read function know about particular socketDescriptor of socket if we have multiple client connected .Because in that situation there are more than one socket are connected. (socketDescriptor is initialized in header file)

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

          @JadeN001
          Given a QTcpSocket, from http://doc.qt.io/qt-5/qabstractsocket.html there are methods like localAddress/Port() & peerAddress/Name/Port() to see what the socket is connected to at server/client sides.

          Otherwise, from the "native socket descriptor" passed to socket->setSocketDescriptor(this->socketDesc), I assume this is the native "SOCKET" type you can access the struct sockaddr_in to get this information.

          1 Reply Last reply
          0
          • Gojir4G Gojir4

            Hi,

            In your "Read()" slot, you can retrieve the QTcpSocket by using sender().

            void MyClass::Read() //Read slot
            {
                QTcpSocket * socket = static_cast<QTcpSocket *>(sender());
                if(!socket)
                     return; //Problem to retrieve the socket, ususally should not happen.
            
                QByteArray data = socket->readAll();
                //...
            }
            
            J Offline
            J Offline
            JadeN001
            wrote on last edited by
            #5

            @Gojir4
            QTcpSocket * socket = static_cast<QTcpSocket *>(sender());
            its not working, it stops my server and returns socket value as QObject(0X0). what could be the issue here?

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Just create a worker instance per connection so you can handle the incoming data separately and know every time where the data is coming from.

              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
              2

              • Login

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