[Solved] - [QTcpSocket] Identify which client sent data to server



  • Hi,

    I'm currently writing a client-server application using [QTcpServer] and [QTcpSocket]. The server can be connected with several client at the same time and the data can be sent from server to client and vise verse. Let say 3 clients : A, B, and C are currently connected to the server and A sent a data to server, server are notified by ReadyRead() signal and perform some function but how can I identify which client sent the data to server. I can post my code snippet if necessary. Thank you.



  • I guess what you are doing is connect the readyRead() signal of all the sockets to the QTcpServer and want to know which particular socket emitted the signal?

    In that case you can maybe use this:

    http://doc.qt.io/qt-5/qobject.html#sender

    after which can for example use the socketdescriptor to identify the socket.

    Hope that helps.



  • Hi,
    Yes i connected the ReadyRead() signal to a function slot whenever a newConnection from client is established to server and keep a copy of the *QTcpSocket of the client in a QList. Aside from using the function sender() you suggested, any other solution which I can utilize from the QList of *QTcpSocket which i have keep in the server. Thank you.



  • Hi,

    You actually need both the QList and the pointer that sender returns to determine which socket emitted the signal.

    Greetings



  • Thank for your reply, may I know generally how to implement proper client server application that can handle multiple clients at the same time. the Fortune client and server example of the QT doesn't demonstrate multi clients configuration.



  • It really depends on what you want to do and the data the server and socket are exchanging.


  • Moderators

    [quote author="blaze_spirit" date="1418386072"]Thank for your reply, may I know generally how to implement proper client server application that can handle multiple clients at the same time. the Fortune client and server example of the QT doesn't demonstrate multi clients configuration. [/quote]

    The example is in that respect a bit misleading.
    @
    void Server::sendFortune()
    {
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_0);
    out << (quint16)0;
    out << fortunes.at(qrand() % fortunes.size());
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));

    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    connect(clientConnection, SIGNAL(disconnected()),
            clientConnection, SLOT(deleteLater()));
    
    clientConnection->write(block);
    clientConnection->disconnectFromHost();
    

    }
    @
    The clientConnection is used only once and dumped. For the demonstrator it the way to do. However, there is no requirement otherwise to dump the TCP socket. You can simply keep it and store it somewhere (e.g. QList as suggested by t3685).



  • Basically what I want to do is to write an server-client application to handle simple remote desktop users session. The server application will keep a list of clients that connected to it and queue them up accordingly. Lets say there are 3 clients currently connected to the server application, (namely A,B, and C). Server application will queue them up and allow the first client to use the remote desktop session. After A has done, server will notify B and let it use the remote desktop session. Same goes for the rest of the connected client.
    What I have done now is set up a QList to store the QTcpSocket when there are new connection from client. Client can sent data to the server and server need to respond to it. I not sure how to let the server know which client sent the data to it. The server have a connected ReadyRead signal slot to perform the request but it need to know which client perform the request so it can respond to it.



  • How about this?

    @
    void onReadyRead()
    {
    QTcpSocket client = qobject_cast<QTcpSocket>(sender());
    if (client) {
    // Verify that this is the client allowed to interact with your application
    // either via the pointer itself or some property set to the socket.

        // Example:
        if (client == listOfConnections.first()) {
            //go ahead!
        }
    }
    

    }
    @



  • [quote author="ckakman" date="1418471652"]How about this?

    @
    void onReadyRead()
    {
    QTcpSocket client = qobject_cast<QTcpSocket>(sender());
    if (client) {
    // Verify that this is the client allowed to interact with your application
    // either via the pointer itself or some property set to the socket.

        // Example:
        if (client == listOfConnections.first()) {
            //go ahead!
        }
    }
    

    }
    @[/quote]

    I'll try this and see if it works, thank for your reply =) .



  • [quote author="blaze_spirit" date="1418472594"]
    [quote author="ckakman" date="1418471652"]How about this?

    @
    void onReadyRead()
    {
    QTcpSocket client = qobject_cast<QTcpSocket>(sender());
    if (client) {
    // Verify that this is the client allowed to interact with your application
    // either via the pointer itself or some property set to the socket.

        // Example:
        if (client == listOfConnections.first()) {
            //go ahead!
        }
    }
    

    }
    @[/quote]

    I'll try this and see if it works, thank for your reply =) .[/quote]

    This method works, thank you very much =)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.