TCP server, how to identify the clients?
-
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? -
@Merlino
If you look at the https://doc.qt.io/qt-5/qabstractsocket.html page, there are a bunch of methods for accessing thepeerAddress()
,peerPort()
, etc. That gives you a way of knowing who the client is per socket, which I think is what you are looking for? -
@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?).
-
@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.