How do computers communicate with each other?
How do computers communicate?
When you want to communicate over the internet with other computers, you send packets with information to those computers, but how do the computers know which packets are intended for them, and which ones are not? How do they interpret the data? What does a packet even look like? When you first send a packet out to a remote computer on the internet it first hits your local switch/router, then a set of switches/routers in between you and the remote machine, goes through cables under the ocean then it reaches it's destination. Each packet contains the data or part of the data to be sent, which might be anything from binary representing an image to plain text commands.
Here is what a packet looks like:
Data
This is the actual data you are sending over the network to the computer you're communicating with. Usually it's either binary or text and it can be commands being sent over or just data like a file. HTML for example, is a text-based protocol that works over TCP and here is what the data contents of an HTML request would look like in a packet:
GET / HTTP/1.1
Host: ask.metafilter.com
User-Agent: curl/7.47.0
It's just text!
If it was data being transmitted, like a JPEG image the whole image would be broken into multiple packets before being sent to the other computer. The receiving computer on the other end would then re-assemble the packets upon arrival and get the image.
Ethernet Header
The protocol governing local networking and who gets what packet at the local level is Ethernet. Each computer has a MAC address identifying it uniquely and this is used to talk to them when you are in the same network as them. This layer is known as layer 2 and simply specifies a source and destination MAC address for the packet along with the data. Each packet has an Ethernet header with a source and destination MAC to specify which computer the data was meant for.
IP Header
For the packet to go to the outside world, it needs to be processed by a router that knows how to route that packet outside the local network. While Computers in the same local network can identify each other by MAC addresses, when communicating with other machines on the outside you identify them by their IP addresses, this layer is called layer 3. Packets have an IP header which has source and destination IP dresses, routers then use this header to direct packets to the correct external network.
UDP/TCP Header
When you send a packet to a computer, how does it know which application should handle that packet? That's where the UDP/TCP layer comes in. It introduces the concept of ports, which applications use to register with the system. After an application binds to a port it will receive packets that are destined to that port and ignore packets for other ports. The network stack redirects packets to the correct application by looking at the TCP/UDP header of the packet and deciding which application it was meant for.
TCP v.s. UDP
The difference between TCP and UDP is that TCP is reliable, meaning that it will do a couple of extra things for you in order to handle problems while UDP will simply just send the data out and hope it gets there. Now, what makes TCP actually reliable? When you send a packet, due to network congestion and other environmental issues the packets might arrive out of order, they might get lost, or worst yet...They might comprise a bigger piece of data which was split into different packets and you are now only able to partly reconstruct this data. TCP offers packet re-ordering, handling of fragmentation of larger packets and retries of lost packets to handle all of these issues.
When to use UDP?
UDP has a couple of disadvantages, one of them which I did not mention above being that you can only send a limited amount of data per packet since UDP does not handle fragmentation, but when is it advantageous to use it? Well, if you need a constant fast stream of data, in which missing a couple of packets is not critical then UDP is the way to go. Think video streaming, Voice over IP, medical devices, etc...
very cool I am currently studying for my comptia A+ certification exam and love everything about computers keep up all the good content!