I understand that a port number helps the operating system send network data to the right program. Is the port an actual physical thing, or just a number? Can another program use the same port or read its traffic?
What is a network port physically, and can two programs use one?
Answers
A TCP or UDP port is not a physical socket on your computer. It's a number carried in a field in the TCP or UDP header. The operating system uses that number, along with other information, to work out which socket should receive incoming data.
Think of the IP address as helping get data to the right network endpoint, and the port as helping find the right service there. The analogy isn't perfect. A machine can have multiple IP addresses, and a port number doesn't permanently belong to one program.
Reply to this answer
When a program binds a socket, it asks the operating system to associate it with a local address and port. Whether another program can bind the same port depends on the protocol, addresses, operating system and socket options. It's not always one program per port, but you can't assume sharing will work either.
TCP connections can also share a server's listening port. The OS distinguishes connections using the local and remote IP addresses and ports, so a web server can handle many clients on the same port.
Reply to this answer
Using the same port and seeing another program's traffic are different things. A packet capture tool with suitable permissions can observe traffic on an interface without being the program receiving it. Encryption may prevent it from reading the contents, though.
Simply knowing a port number doesn't normally let an unrelated program read data from another program's socket.
Reply to this answer
Some port numbers are commonly associated with particular services, but the number itself doesn't enforce what a program does. A service can be configured to use a different port, as long as clients know where to connect.
Reply to this answer
Add your answer
Answers are reviewed before publication.
d318~papertray_6:
TCP and UDP have separate port spaces, too. TCP port 8080 and UDP port 8080 are not the same socket.