🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

listen, accept and port determination (TCP)

Started by
2 comments, last by hplus0603 5 years, 2 months ago

I originally thought that when listening on a port, that all accepted connections would be on their own unique local ports.

 

However, recently I was reading someone say that this isn't necessarily the case and the only thing unique was the ip+port combination.

 

This got me wondering - is it possible that when you accept a new connection, that the local port assigned for that socket to use is the same as the port being used to accept new connections?

Also, is it possible that two new connections would be accepted that are using the same local port (but different endpoint ips)?

 

Thanks

Advertisement

 

2 hours ago, pindrought said:

However, recently I was reading someone say that this isn't necessarily the case and the only thing unique was the ip+port combination.

It is the source, and destination port+IP.

2 hours ago, pindrought said:

This got me wondering - is it possible that when you accept a new connection, that the local port assigned for that socket to use is the same as the port being used to accept new connections?

It actually always does, and this is part of how the client computer, and any firewalls, NAT routers, etc. identify the packets as being for that connection. You can see this in packet capture tools like say Wireshark.

You will see the first packet of a TCP connection from the client have a random ephemeral port (or dynamic. Usually from a pool somewhere around 32768 to 65535 for modern OS) to the intended server port (e.g. 80 for HTTP or 443 for HTTPS) . The response packets then use the same port numbers in reverse (e.g. from 80 or 443 to that random port).

2 hours ago, pindrought said:

Also, is it possible that two new connections would be accepted that are using the same local port (but different endpoint ips)?

Yes, but either at different times, or the destination (client) port will be different.

A TCP connection is uniquely identified by the five-tuple (global-time, source-ip, source-port, destination-ip, destination-port)

If you only consider a particular point in time, you're left with the four-tuple (source-ip, source-port, destination-ip, destination-port)

When a client connects to host:port, that fixes the destination-ip and destination-port, These do not change when the server accepts the incoming connection -- the connection will always have (destination-ip, destination-port) as part of the tuple.

Given that the source client has a given IP address, that fixes the source-ip part of the tuple, which only leaves the source-port as the variable part to tell apart multiple connections to the same target:port from that same source host. This is why the connect() call allocates a "random, unused" port number for the client-side socket when connecting, and then keeps that for the duration of the connection.

If you are in a datacenter, and generate a lot of connections from the same source host to the same target host (for example, for an internal HTTP service, or database connection without connection pooling,) then it's possible that you use all source ports on the client machine for the given target host:port. Different OS-es use between 16k and 64k space for these "ephemeral" ports, and a port cannot be re-used within two minutes of the connection ending, because of the TCP spec. This is because a "late packet" from a previously connection should not be confused with a packet for an existing connection between the two hosts. So, the maximum sustained number of connections made and finished between two hosts is somewhere between 136 and 546 connections per second. If this ends up being a problem for you, the first thing to do is probably to turn on connection pooling or something similar; if you can't do that for some reason (looking at you, Ruby and PHP,) then you may need to create more IP addresses for the same host on the same physical interface, using interface aliases, and round-robin outgoing connections across these interface aliases. Here's hoping your game will be so successful that you'll have to worry about this ?

 

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement