4

When researching something, I often try to come to my own conclusions based on the information I take in. I have tried to answer my own question but just want clarification.

Ports are needed so that traffic coming from different applications on different sources can simultaneously reach the same host. For example, http traffic on one port (port 80) and telnet traffic on another (port 23). And when there is more than one source of a particular type of traffic for one host, then a new port is created for that traffic that will only link to that host.

Right? Wrong?

2 Answers 2

7

The simplest explanation is that ports identify what process on the host the received traffic should be sent to. A computer can have multiple simultaneous connections, all receiving data for different processes (mail, web, database, etc) on that computer. How does the computer tell which data goes where?

When the computer receives data, the port information allows it to give the data to the correct process. For example, data with port 80 should go to the http process. Data with port 25 should go to the mail process, and so on.

In other words, the IP address identifies the computer host, and the port number specifies the particular process running on that host. It's like a street address identifies a building, but the room number identifies a particular apartment or office.

11
  • Do ports reside on both the router and the host in a LAN? And if so do new logical ports need to be created on the dest host if there is more than one source of traffic for a specific port on the dest host? E.g. If if were accessing two different websites at the one time, would my computer create two different ports for the http traffic?
    – l30n1d45
    Commented Mar 26, 2015 at 15:34
  • Routers operate at layer 3, so they normally do not look at layer 4 (port) information.
    – Ron Trunk
    Commented Mar 26, 2015 at 15:35
  • Ah so ports only really operate on the host. What about the second part of my question? If there are two sources of the same type of traffic for one host, does the host create two ports for each source?
    – l30n1d45
    Commented Mar 26, 2015 at 15:37
  • Only one destination port (80, for example). Each source can be identified by IP address and source port number. If you have multiple connections from the same host (typical of browsers), each one will have a different source port number.
    – Ron Trunk
    Commented Mar 26, 2015 at 15:37
  • So when I send http traffic, it will go through a made up port number, say 1234, but the destination port will be 80? And when I get http traffic back, will it come from the destinations own made up port number or still port 80? @Ron
    – l30n1d45
    Commented Mar 26, 2015 at 15:41
1

The keyword here is socket.

A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to.

On most operating systems, SOCKETS are identified by a number, similar to a file descriptor, that index a single entry into a table containing information about a connection. This information is usually in the following format:

SOURCEIP:SOURCEPORT DESTINATIONIP:DESTINATIONPORT PROTOCOL STATE

This table can be accessed, generally, via the NETSTAT command (or its equivalent) on most operating systems. In no event you can have two entries on such table with equal values (in other words: two entries with the exact same sourceip, sourceport, destinationip, destinationport and protocol at the same time). You can have two entries with same destination ports, source ports etc, any value, but never exact same values on two or more entries. And each entry is indexed (identified) by a single socket number. (There are exceptions to this rule)

This socket number is returned when you issue a SOCKET() function call. (on linux/freebsd/windows). Later, your program will decide what to do with the socket requested from the operating system. It can issue a CONNECT() call and connect this socket into a remote machine or a BIND() and LISTEN() calls to use it to wait for inbound connections.

This means that two processes CAN share a port, if they happen to share the SOCKET associated to the port. This is even a common way to do network programming. You can fork or spawn another process when your listening socket receives a connection request and you call ACCEPT() to accept it. ACCEPT() will return a socket number that will identify a new SOCKET created for that incomming connection, you will then pass the socketnum to the spawned/forked process.

For example on Windows:

The WSADuplicateSocket function is introduced to enable socket sharing across processes. A source process calls WSADuplicateSocket to obtain a special WSAPROTOCOL_INFO structure for a target process identifier. It uses some interprocess communications (IPC) mechanism to pass the contents of this structure to a target process. The target process then uses the WSAPROTOCOL_INFO structure in a call to WSPSocket. The socket descriptor returned by this function will be an additional socket descriptor to an underlying socket which thus becomes shared. Sockets can be shared among threads in a given process without using the WSADuplicateSocket function because a socket descriptor is valid in all threads of a process.

In other words, ports are used to help identify SOCKETS on a computer, which are single connections between TWO hosts on the network. This is true for both TCP and UDP protocols. They do not, usually, identify processes and most operating systems wont bother if two processes act on the same port. The keyword here is SOCKET, not process. Sockets are the things that must be unique, because they identify a unique connection between two hosts.

So, answering your question. Why are ports needed ?

Because if there were no ports, the table i refered to in the start of the text would be limited to:

SOURCEIP DESTINATIONIP PROTOCOL STATE

In other words, you could have only a single SOCKET for each host accessible to your computer, wich is not very usefull at all.

Sources:
What Is a Socket?
Sockets Tutorial
Shared Sockets

6
  • So on a host, a socket consists of a single ip address and a port, pretty much identifies an end point between two network applications and the number of sockets on the host is directly proportionate to the number of ports? @Jorge Aldo
    – l30n1d45
    Commented Mar 27, 2015 at 9:50
  • 1
    its proportional to the number of ports and ips
    – Jorge Aldo
    Commented Mar 27, 2015 at 13:27
  • But there can't be more than one ip on a host since there's multiple ports on a host, how can they be proportional to the ip? @Jorge Aldo
    – l30n1d45
    Commented Mar 27, 2015 at 23:35
  • 1
    You CAN have more than one IP on a host, either by having more than a network card or having multiple IPs on the same card. How to configure that depends on the OS at hand. But its way easier to achieve multiplexing via ports than via multiple IPs, thats why ports are used both on TCP and UDP.
    – Jorge Aldo
    Commented Mar 28, 2015 at 0:42
  • 1
    same set of sockets and no, not necessarely a bridge
    – Jorge Aldo
    Commented Mar 28, 2015 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.