Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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.?



  • @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?



  • @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 !!?



  • 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



  • @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



  • @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.



  • @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 .



  • @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



  • @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.



  • @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



  • @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 ?


  • Qt Champions 2017

    Have you looked at the examples?



  • @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).



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


Log in to reply