egoalter

egoalter t1_j228faf wrote

All communication on a TCP/IP network needs a socket pair for communication for destination and source. This means IP/PORT for both sender and receiver. They are equally important to make communication work because a computer representing an IP run more than one thing at a time, but each process that runs need a unique transmission pipe. That's the sender port. The destination is "what service" do you want - in general servers have many programs running to handle different incoming traffic. In other words, an IP by itself doesn't work. Just like the type of protocol can be different. Some protocols use different ways of addressing the service (such as ICMP) but in general the sender specifies what service the data is meant for.

A bit more technical, when you write server code that responds to incoming network traffic you bind/select a given port, telling the OS that traffic that comes in to this port is responded to by your process. Only ONE process can do this at a time. Some ports are restricted and a lot of ports have been standardized (see /etc/services on Linux) but to the computer the number is arbitrary. You can easily make your web-server respond to incoming requests to port 7777 if you want. It's just a number.

The sender's port number is very rarely specified manually (but you can). The OS provides a pool of free sender port numbers and assigns a "random" port number when you open a connection. Every time you open a connection a new port is created, and if the programmer forgets to close the connection the OS will quickly run out of port numbers in the pool. But the IP/port of the source/destination stays the same throughout the whole communication. A lot of this is small/quick communication - when you browse a web-page you're often directed to 10,20 or more sites for style, scripts and more - each of them the browser creates a separate connection to (although modern protocols can share connections to the same server). Point is, that the socket pairs are key to identify the connection.

1