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. TCP server, how to identify the clients?
QtWS25 Last Chance

TCP server, how to identify the clients?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 708 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.
  • M Offline
    M Offline
    Merlino
    wrote on 6 Aug 2020, 09:54 last edited by aha_1980
    #1

    Hello,
    I have a TCP server which could have multiple clients. Each client sends messages, declaring the message total length in the first four bytes. Due to TCP framing the message could be broken up into parts, so I need to store it in a temporary map (socket->data) and process the message only when all the fragments have received.

    For this reason I would need a unique client identifier (socket) to be able to use it as a key.
    I currently use the pointer, but it seems like crap and definitely not the best method. I have tried if there is a method that returns the client data but was unable to find it. Any suggestions?

    J 1 Reply Last reply 6 Aug 2020, 10:05
    0
    • M Merlino
      6 Aug 2020, 09:54

      Hello,
      I have a TCP server which could have multiple clients. Each client sends messages, declaring the message total length in the first four bytes. Due to TCP framing the message could be broken up into parts, so I need to store it in a temporary map (socket->data) and process the message only when all the fragments have received.

      For this reason I would need a unique client identifier (socket) to be able to use it as a key.
      I currently use the pointer, but it seems like crap and definitely not the best method. I have tried if there is a method that returns the client data but was unable to find it. Any suggestions?

      J Offline
      J Offline
      JonB
      wrote on 6 Aug 2020, 10:05 last edited by
      #2

      @Merlino
      If you look at the https://doc.qt.io/qt-5/qabstractsocket.html page, there are a bunch of methods for accessing the peerAddress(), peerPort(), etc. That gives you a way of knowing who the client is per socket, which I think is what you are looking for?

      M 1 Reply Last reply 6 Aug 2020, 10:36
      3
      • J JonB
        6 Aug 2020, 10:05

        @Merlino
        If you look at the https://doc.qt.io/qt-5/qabstractsocket.html page, there are a bunch of methods for accessing the peerAddress(), peerPort(), etc. That gives you a way of knowing who the client is per socket, which I think is what you are looking for?

        M Offline
        M Offline
        Merlino
        wrote on 6 Aug 2020, 10:36 last edited by
        #3

        @JonB Thank you for your anwer, I've found the peerName() peerAddress() methods, but what if I have multiple clients from the same address/port?

        J J 2 Replies Last reply 6 Aug 2020, 10:44
        0
        • M Merlino
          6 Aug 2020, 10:36

          @JonB Thank you for your anwer, I've found the peerName() peerAddress() methods, but what if I have multiple clients from the same address/port?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 6 Aug 2020, 10:44 last edited by
          #4

          @Merlino said in TCP server, how to indentify the clients?:

          if I have multiple clients from the same address/port

          I don't think this is possible as only one client can have a given ip/port combination (same port can't be used at the same time by more than one client - how else would the communication work?).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • M Merlino
            6 Aug 2020, 10:36

            @JonB Thank you for your anwer, I've found the peerName() peerAddress() methods, but what if I have multiple clients from the same address/port?

            J Offline
            J Offline
            JonB
            wrote on 6 Aug 2020, 11:23 last edited by
            #5

            @Merlino said in TCP server, how to indentify the clients?:

            but what if I have multiple clients from the same address/port?

            You can't :) In TCP/IP for a socket the following:

            serverIP + serverPort + clientIP + clientPort

            has to be unique, you cannot have duplicates!

            Have a go at it. If you connect a second time from the same client, you'll see that the clientPort number changes.

            1 Reply Last reply
            4
            • M Offline
              M Offline
              Merlino
              wrote on 6 Aug 2020, 12:23 last edited by
              #6

              Thank you everybody for the answers :)

              1 Reply Last reply
              0

              2/6

              6 Aug 2020, 10:05

              4 unread
              • Login

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