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?
Forum Updated to NodeBB v4.3 + New Features

TCP server, how to identify the clients?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 808 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 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?

    JonBJ 1 Reply Last reply
    0
    • M Merlino

      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?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on 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
      3
      • JonBJ JonB

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

        jsulmJ JonBJ 2 Replies Last reply
        0
        • M Merlino

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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

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

            JonBJ Online
            JonBJ Online
            JonB
            wrote on 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 last edited by
              #6

              Thank you everybody for the answers :)

              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