I can’t come up with the exact socket api,but I remember that there is a socket option that demonstrates the port exclusive/non-exclusive.
If it’s not exclusive,how can TCP know which application it should forward to for a specific destination port?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think you might be referring to the
SO_REUSEPORToption available on some systems.From the BSD man page:
Implementations vary a lot for this (from non-existant, to restricted to UDP, to allowing TCP also). In the cases where TCP is allowed, the connexions are distinguished by both source and target (ip,port) pairs. This is sufficient to allow the implementation to decide which app needs which packet. (see Trek – Socket options for instance.)
With multiple apps bind to the same TCP port, you could only have one socket
accepting on the port. The others would use the port to initiate outbound connections. The TCP stack always knows where to send the packets to.Note: the sockets themselves (including, I believe the accepting socket) can be shared across multiple processes. See Is there a way for multiple processes to share a listening socket? for example.
Here’s how it could work. Voluntarily simplifying TCP (no three-way handshake). Let’s note the socket information held by the TCP stack like this
With that, let’s set up three apps A, B and C:
At this point, no data has been sent, but the kernel knows exactly what socket belongs to what application. Let’s have A actually try to do something with its socket:
Here, three things can happen:
Let’s imagine the two things above happened. The kernel information is now:
Now four kinds of packets can come in:
The TCP stack always nows which socket a packet belongs to. So it knows where to deliver the data.
SO_REUSEADDRis different: it only allows to bind to a port inTIME_WAITstate – i.e. the port is already closing down, the socket that had opened it has been issued aclosealready (more or less, there are other conditions). WithSO_REUSEADDR, only one socket at a time holds the socket open.