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 does readyRead() know about which socket is to read?
Forum Updated to NodeBB v4.3 + New Features

How does readyRead() know about which socket is to read?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 5.7k 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.
  • M Offline
    M Offline
    MokJ
    wrote on last edited by
    #1

    I want to know that how readyRead() works. How it knows that this socket is about to read data if there are multiple clients. How does it make sure that this is the socket descriptor of the socket its about to call.?

    JonBJ 1 Reply Last reply
    0
    • M MokJ

      I want to know that how readyRead() works. How it knows that this socket is about to read data if there are multiple clients. How does it make sure that this is the socket descriptor of the socket its about to call.?

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

      @MokJ
      QIODevice::readyRead()? QTcpSocket::readyRead()?

      When you talk about "if there are multiple clients", are you asking about UDP sockets, or are you asking about TCP sockets set up with a server which accepts multiple client connections and spawns a separate socket of each one?

      M 1 Reply Last reply
      1
      • JonBJ JonB

        @MokJ
        QIODevice::readyRead()? QTcpSocket::readyRead()?

        When you talk about "if there are multiple clients", are you asking about UDP sockets, or are you asking about TCP sockets set up with a server which accepts multiple client connections and spawns a separate socket of each one?

        M Offline
        M Offline
        MokJ
        wrote on last edited by
        #3

        @JonB
        QTcpSocket::readyRead()
        I am trying to build a client server chat app kinda thing. what i want to know is
        when I do this on server side:: connect(socket,SIGNAL(readyRead()),this,SLOT(readData()));

        so every readyRead() of the socket will get connected to the readData(), my question is how readyRead () will differentiate sockets?
        How will socket->readAll(); will get called for different sockets,
        socket in socket->readAll(); will be the new socket created by server ??
        I mean on server side, how to differentiate which socket is reading !!?

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

          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();
              //...
          }
          

          I think this is the same question than here: https://forum.qt.io/topic/90324/how-readyread-know-about-from-which-socket-it-is-called

          M 1 Reply Last reply
          2
          • 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();
                //...
            }
            

            I think this is the same question than here: https://forum.qt.io/topic/90324/how-readyread-know-about-from-which-socket-it-is-called

            M Offline
            M Offline
            MokJ
            wrote on last edited by
            #5

            @Gojir4
            yeah Thanks very much

            so if I do QTcpSocket socket=(QTcpSocket)sender();
            its the same as you've shown !!!

            again if it is, what if I don't use it and just do socket->readAll();
            so in this case what socket are we talking about ?
            again I'm talking about server side only.
            Thanks again @Gojir4

            Gojir4G 1 Reply Last reply
            0
            • M MokJ

              @Gojir4
              yeah Thanks very much

              so if I do QTcpSocket socket=(QTcpSocket)sender();
              its the same as you've shown !!!

              again if it is, what if I don't use it and just do socket->readAll();
              so in this case what socket are we talking about ?
              again I'm talking about server side only.
              Thanks again @Gojir4

              Gojir4G Offline
              Gojir4G Offline
              Gojir4
              wrote on last edited by
              #6

              @MokJ does what you mean is how to identify the socket which sent the signal ? As you are at server side you should know the clients which are connected to the server.

              M 1 Reply Last reply
              0
              • Gojir4G Gojir4

                @MokJ does what you mean is how to identify the socket which sent the signal ? As you are at server side you should know the clients which are connected to the server.

                M Offline
                M Offline
                MokJ
                wrote on last edited by
                #7

                @Gojir4 yeas thats exactly is my point.
                I want to make sure on server side that which socket has sent the signal or message .
                Thanks for quick response .

                Gojir4G 1 Reply Last reply
                0
                • M MokJ

                  @Gojir4 yeas thats exactly is my point.
                  I want to make sure on server side that which socket has sent the signal or message .
                  Thanks for quick response .

                  Gojir4G Offline
                  Gojir4G Offline
                  Gojir4
                  wrote on last edited by
                  #8

                  @MokJ so when you accept the connection from the socket, you can store it in a list or a map, with the socket descriptor or another id. Then retrieve it from the list when readyRead is called. Or you can get inspiration from Qt examples like this one http://doc.qt.io/qt-5/qtnetwork-network-chat-example.html. im sorry im on the phone so i cannot provide some code sample

                  M 1 Reply Last reply
                  0
                  • Gojir4G Gojir4

                    @MokJ so when you accept the connection from the socket, you can store it in a list or a map, with the socket descriptor or another id. Then retrieve it from the list when readyRead is called. Or you can get inspiration from Qt examples like this one http://doc.qt.io/qt-5/qtnetwork-network-chat-example.html. im sorry im on the phone so i cannot provide some code sample

                    M Offline
                    M Offline
                    MokJ
                    wrote on last edited by
                    #9

                    @Gojir4 thanks very much . I'd look into it.
                    and If you can give any clue or document about how readyRead() works internally , that'd be a big help.

                    JonBJ 1 Reply Last reply
                    0
                    • M MokJ

                      @Gojir4 thanks very much . I'd look into it.
                      and If you can give any clue or document about how readyRead() works internally , that'd be a big help.

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #10

                      @MokJ , @Gojir4

                      I don't really understand, I think there are two separate threads on this issue... ??

                      I have already answered how you can detect client in https://forum.qt.io/topic/90324/how-readyread-know-about-from-which-socket-it-is-called

                      M 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @MokJ , @Gojir4

                        I don't really understand, I think there are two separate threads on this issue... ??

                        I have already answered how you can detect client in https://forum.qt.io/topic/90324/how-readyread-know-about-from-which-socket-it-is-called

                        M Offline
                        M Offline
                        MokJ
                        wrote on last edited by
                        #11

                        @JonB I wouldn''t know about the other thread , But this is what I'm having trouble with and I'm definitely trying to apply all the suggestion to get the solution.
                        and again talking about readyRead(), If I create a new QTcpSocket to read , just read at server side, I mean an unique socket just for reading , is it possible ?

                        kshegunovK JonBJ 2 Replies Last reply
                        0
                        • M MokJ

                          @JonB I wouldn''t know about the other thread , But this is what I'm having trouble with and I'm definitely trying to apply all the suggestion to get the solution.
                          and again talking about readyRead(), If I create a new QTcpSocket to read , just read at server side, I mean an unique socket just for reading , is it possible ?

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #12

                          Have you looked at the examples?

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          3
                          • M MokJ

                            @JonB I wouldn''t know about the other thread , But this is what I'm having trouble with and I'm definitely trying to apply all the suggestion to get the solution.
                            and again talking about readyRead(), If I create a new QTcpSocket to read , just read at server side, I mean an unique socket just for reading , is it possible ?

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by JonB
                            #13

                            @MokJ
                            There must be two similar threads on-going with the same question then!

                            I'm not sure what you are asking now, but if you want to know "which client is a given socket connected to" I stand by my statement in the other thread:

                            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.

                            Also, just to be clear if you are not sure about this, with TCP sockets if you have multiple clients connected to one server each connection has a distinct, separate connected socket. So when you call socket->readRead() or socket->readAll() the socket is for one particular connected client (the one when the QTcpSocket was initially created).

                            1 Reply Last reply
                            2
                            • M Offline
                              M Offline
                              MokJ
                              wrote on last edited by
                              #14

                              yes @kshegunov I have ,
                              Thanks @JonB
                              I got it now

                              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