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. [Solved] - [QTcpSocket] Identify which client sent data to server

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

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 5.6k 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.
  • T Offline
    T Offline
    t3685
    wrote on last edited by
    #2

    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.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      blaze_spirit
      wrote on last edited by
      #3

      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.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        t3685
        wrote on last edited by
        #4

        Hi,

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

        Greetings

        1 Reply Last reply
        0
        • B Offline
          B Offline
          blaze_spirit
          wrote on last edited by
          #5

          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.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            t3685
            wrote on last edited by
            #6

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

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #7

              [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).

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • B Offline
                B Offline
                blaze_spirit
                wrote on last edited by
                #8

                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.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ckakman
                  wrote on last edited by
                  #9

                  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!
                      }
                  }
                  

                  }
                  @

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    blaze_spirit
                    wrote on last edited by
                    #10

                    [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 =) .

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      blaze_spirit
                      wrote on last edited by
                      #11

                      [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 =)

                      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