Overview
a port is a software construct serving as a communications endpoint in a computer’s host operating system.
purpose of ports is to uniquely identify different applications or processes running on a single computer and thereby enable them to share a single physical connection to a packet-switched network like the Internet.
The protocols that primarily use ports are the Transport Layer protocols, such as TCP and UDP.
Port info can be viewed on Linux /etc/services files.
there’re only 65536 ports
In TCP/IP stack, port number field is just 16bit size unsigned integer. Port number thus ranging from 0 to 65535.
well-known ports
Well-known ports (or Privileged Ports) are those from 0 through 1023.
- 20 & 21: File Transfer Protocol (FTP)
- 22: Secure Shell (SSH)
- 23: Telnet remote login service
- 25: Simple Mail Transfer Protocol (SMTP)
- 53: Domain Name System (DNS) service
- 80: Hypertext Transfer Protocol (HTTP) used in the World Wide Web
- 110: Post Office Protocol (POP3)
- 119: Network News Transfer Protocol (NNTP)
- 143: Internet Message Access Protocol (IMAP)
- 161: Simple Network Management Protocol (SNMP)
- 194: Internet Relay Chat (IRC)
- 443: HTTP Secure (HTTPS)
- 465: SMTP Secure (SMTPS)
Socket
Socket is combination of software Port and IP address.
Protocol number
In an IP header, the Protocol field identifies the service in the next higher level in the protocol stack to which data is passed. Do not confuse this with port number, which is used for communication by TCP/UDP.
Service |
Protocol Number |
---|---|
Internet Control Message Protocol (ICMP) |
1 |
Transmission Control Protocol (TCP) |
6 |
User Datagram Protocol (UDP) |
17 |
General Routing Encapsulation (PPTP data over GRE) |
47 |
Authentication Header (AH) IPSec |
51 |
Encapsulation Security Payload (ESP) IPSec |
50 |
Exterior Gateway Protocol (EGP) |
8 |
Gateway-Gateway Protocol (GGP) |
3 |
Host Monitoring Protocol (HMP) |
20 |
Internet Group Management Protocol (IGMP) |
88 |
MIT Remote Virtual Disk (RVD) |
66 |
OSPF Open Shortest Path First |
89 |
PARC Universal Packet Protocol (PUP) |
12 |
Reliable Datagram Protocol (RDP) |
27 |
Reservation Protocol (RSVP) QoS |
46 |
When the IP packet contain TCP data the protocol number field will have the value 6 in it, so the payload will be sent to the TCP stack, TCP would then use the port numbers to send the data to the correct application. The same is for UDP with protocol number 17.
Another way to look at the IP protocol number field is, if we didn’t have this field in the IP packet header, IP would only be capable of carrying one type of data, while adding this field allowed the IP to carry multiple types of data differentiated by the protocol number, the same goes for TCP/UDP using TCP/UDP ports to serve multiple applications and Ethernet using the Ethertype, and so on.
can multiple app bind to (or listen to) the same port?
Can’t. Because You can only have one application listening on a single port at one time.
the app opens a port, gets a handle to it, and the OS notifies it (via that handle) when a client connection (or a packet in UDP case) arrives.
If the OS allowed two apps to open the same port, how would it know which one to notify?