Port and Socket
Created at 2016-01-05 Updated at 2023-03-08 Category experience / work
A socket is one of two endpoints of a communication link between two computers (or occasionally between one computer and itself) across a network. That communication link may use an common, established protocol (such as TCP) or may use a totally proprietary protocol.It represents a single connection between two network applications. These two applications nominally run on different computers, but sockets can also be used for interprocess communication on a single computer. Applications can create multiple sockets for communicating with each other. They are bidirectional, meaning that either side of the connection is capable of both sending and receiving data.
We use TCP or UDP ports, where each port represents a single service. A server process listens on a port and accepts incoming connections (TCP) or incoming packets (UDP). The client allocates a local port number for initiating the connection. The TCP connection is identified by the IP address and port pair.
The purpose of ports is to differentiate multiple endpoints on a given network address. You could say that a port is a virtualised endpoint. This virtualisation makes multiple concurrent connections on a single network interface possible.
A port is a service provided by a machine. That service is identified by a number. There are a lot of standardized port numbers for popular services. Port 22 is for FTP. Port 80 is for HTTP. There are many, many others.
A socket is the way a server and a client keep track of requests. For example, localhost allows access to port 80 (HTTP) for the purposes of reading these forums. When my computer requests to see the content of the forums, a socket is created, and a “conversation” between the server and my machine occurs. The thing is, I’m not the only person trying to read this blog. So, to keep the server from getting conversations mixed up, each request gets a different socket.
So, ports are a broad reference to a type of service, whereas a socket refers to a specific connection on a specific port.