Reputation: 63720
I know that multiple TCP clients can connect to the same remote endpoint (e.g. my server runs on 127.0.0.1:8080).
I know that multiple TCP clients can connect from the same IP address. But when I test this (in my case using .Net's TcpClient
class, they seem to be automatically given unique ports, but is that enforced?
Can my TCP listener/server have multiple concurrent connections from the same IP/port combination? If not, how can I uniquely distinguish my connections?
One edge case I considered is if two clients have the same IP4 address... or on any given network is IP4 uniqueness also enforced?
Upvotes: 3
Views: 3835
Reputation: 402
Given the TCP/IP stack definition, no, you cannot enforce which port a client listens on from the server side. When a client hits a server it sends a port number that it expects the server to respond to (the client will listen on this port) called a Source Port. The server responds to that unique port for that particular client.
https://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/
Looking at this capture you can see the client sent a packet with the request to (Destination) port 80 on the server with a (source) port 54841. This 54841 should be unique to that client from the servers perspective especially when combined with the Client IP address.
In more detail, the client sends a packet like this:
IP: Src: 192.168.1.23 (client), Dst: 192.168.1.11 (server)
TCP: Src Port: 54841, Dst Port: 80
The server responds with a packet that looks like this:
IP: Src: 192.168.1.11 (server), Dst: 192.168.1.23 (client)
TCP: Src Port: 80, Dst Port: 54841
Hopefully you can see that the server is able to uniquely track each request by the client IP/Port combination. A new request from the same client would have the same IP but a different Port.
IPv4 address conflicts are a problem for most networks. Most networks will not tolerate an IP address conflict. It is usually enforce by a competent network administrator or a DHCP server handling the IP addresses. Someone can manually force an IP address on a client and this will cause problems with the other host that has the same IP address. But this seems to be a different question from your original.
Upvotes: 1
Reputation: 414
When speaking about TCP, a connection between two entities is identified by the quadruple
<clientIP, clientPort, serverIP, serverPort>
This is why the same service (running on serverIP, serverPort
) can accept
clientIP
BUT different clientPort
)I'm not sure about .NET's implementation, altough I think it would work anyway, but when estabilishing a connection on the client side, you can also specify the port to which bind your client side socket; this way, on the server sidem you could identify incoming connections connections from the same host by their clientPort value.
Upvotes: 1