eli5: difference between socket and a port

54 views
0

After searching both these things up, I got that in networking, both a socket and a port is a commication endpoint. However, in many articles I’ve read they are used in different ways. Can someone explain key differences between a port and a socket, or provide a good analogy for one?

In: 7

A socket uniquely identifiers an endpoint of a connection. To simplify a bit it is a combination of IP address and port number.

A port is just part of an address. It’s a number – nothing more, like an apartment number or a telephone extension number. The IP address is the other part of the address. All TCP & UDP packets must be addressed to an IP address and port, and have a source IP address and port. The source & destination IP addresses are in the IP packet header and the source and destination port numbers are in the TCP or UDP packet header. Just like how postal workers route letters based on addresses, UDP and TCP packets are routed on the Internet based on the destination IP address and port number. A street address will get the letter to the right building, and the apartment number will get it to the correct apartment inside the building. The destination IP address is used to route the packet to the right computer, and the destination port number is used to route the packet to the right bit of software running inside the computer. Every connection made over the internet has an IP address and port number for each end.

A socket is a software interface to one end of a connection. Sockets are provided by the operating system. Sockets are created when you need a connection and destroyed after the connection is closed. Your application writes to and reads from the socket. Once the connection is established, the socket will be associated with a local IP address and port number, and a remote IP address and port number. A socket is like the cone on the end of a ship’s speaking tube that you shout into or put your ear up to to listen to, or like an analog telephone wall jack that you can plug a phone into. (Not coincidentally, “socket” is another name for a female jack.) A socket is what you plug into to use a connection.

If it was a phone line

The port would be the last part of the phone number you want to call

The socket would kinda be the call that your secretary has made for you from her desk. She calls the number you want to call, sets up the call, and then you pick up the phone on your desk and start talking. Here your secretary is the OS that handles the setting up of the connection. And you are the program that needs to use the connection for whatever purpose.

A socket is the software-part of a port used by server programs to communicate over the internet. The port number identyfies the server program (Web Server on port 80, Mail Server on 25, Minecraft-server, …) running on a computer (which is identified by its IP address).