Photo by Neven Krcmarek on Unsplash
I?m currently in the beginning stages of building an HTTP server in Java, but before getting too far into the implementation, I wanted to cement my understanding of software sockets and what role they play in the client/server relationship.
Like most software terms, a socket is an abstraction of the type of physical socket (like the one pictured above) that we?re all familiar with. A physical socket is an entry point for a power cable. A software socket is also an entry point, but instead of accepting a power cable, it accepts a network connection from another computer.
The very earliest computers had no need for such sockets, because computer networking wasn?t yet a thing. Computers were simply standalone machines that ran processes but couldn?t communicate with other computers. Then systems like arpanet, ethernet, and internet came along to bring us to where we are today, with billions of computing devices all connected to one another from across the globe. But with all those devices talking to one another, how does it all stay organized? The full answer, as usual, is beyond the scope of this post, but the short answer is that sockets play a key role in keeping all these communications neat and tidy.
To understand sockets we first need to explore related technologies that make sockets possible: IP addresses, ports, and TCP/IP.
Every computer has an IP (Internet Protocol) address. This is a unique number comprised of four 8-bit numbers separated by dots. This number identifies your computer when it?s connected to a network using the Internet Protocol (IP) for communication.
When you type a website into a web browser and hit enter, you?re sending a request for that website along with the IP address of your computer so that the server hosting that website can route the contents back to the correct location. However, what happens when you want to open multiple web pages, or multiples of any application that reaches out to other computers over the internet? You only have one IP address, so that alone is not enough to connect multiple requests to the right destination. The answer is our second piece of related technology: ports.
First off, we?re talking about software ports here, not hardware ports. A hardware port is simply a socket that accepts device cables, like the HDMI cable going to your TV or the 3.5mm headphone jack going into your phone (for those that still have headphone jacks).
A software port is a number that identifies the specific application or service on your computer that?s attempting to access the network. IP addresses and ports can be compared to a phone number: the IP address is like the area code, which identifies the general area that the phone call should be routed to. The port is like the rest of the phone number, which will route the call to the specific phone number being requested. The IP address gets the requested information to the right computer, and the port gets the information to the right application or service running on that computer.
In a very over-simplified sense, a socket is simply the combination of an IP address and a port. More formally, 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.?
To see this in action simply click the following link, which is the stripped down way to access google.com: 188.8.131.52:80
This introduces the third technology we?re going to explore in this post: TCP, or Transmission Control Protocol. When you send a large file over the internet, it doesn?t get sent in one big chunk. Instead it gets chopped up into small manageable bits (called packets) that each find their way to the intended destination. This way you?re not blocking a connection and waiting until the entire file is sent to it?s destination. Imagine a train with 100 cars trying to cross a busy intersection. The entire train would need to cross before a car could make its way through the intersection, and all those cars are now delayed. If instead you could (somehow) chop that train up and space the cars far enough apart, you could have cars crossing the road in-between each car, and you?ve just reduced congestion significantly. Packets act in the same way. Big files are chopped up into manageable packets to reduce congestion.
TCP makes sure that all the packets arrive in their correct destination, and can then be re-ordered into the correct sequence, since the packets might take different routes to the same destination. TCP also sends an acknowledgment that the packet was received by the receiver, so if a packet gets lost the sender will know because it never received an acknowledgment, and can then send the packet again.
So to put it all back together, a socket is the combination of an IP address and a port, and it acts as an endpoint for receiving or sending information over the internet, which is kept organized by TCP. These building blocks (in conjunction with various other protocols and technologies) work in the background to make every google search, facebook post, or introductory technical blog post possible.