Introduction to TCP/IP
Reprinted from [Liao Xuefeng Python Tutorial]
Although everyone is now familiar with the Internet, computer networks appeared much earlier than the Internet.
In order to network, computers must specify communication protocols. Early computer networks each had a set of protocols defined by the vendors themselves. IBM, Apple and Microsoft each had their own network protocols, mutually incompatible. This is like a group of people, some speaking English, some speaking Chinese, some speaking German; people who speak the same language can communicate, but not between different languages.
In order to connect all the different types of computers around the world, a set of globally universal protocols must be defined. To achieve the goal of the Internet, the Internet Protocol Suite is the universal protocol standard. Internet is made up of the two words inter and net; its original meaning is a network connecting “networks”. With the Internet, any private network can join the Internet as long as it supports this protocol.
Because the Internet protocols contain hundreds of protocol standards, but the two most important protocols are TCP and IP, everyone abbreviates the Internet protocols as the TCP/IP protocol.
When communicating, both parties must know the other’s identifier, just like sending an email requires knowing the other’s email address. The unique identifier of each computer on the Internet is the IP address, similar to 123.123.123.123. If a computer is connected to two or more networks at the same time, such as a router, it will have two or more IP addresses. Therefore, an IP address actually corresponds to a computer’s network interface, usually a network card.
The IP protocol is responsible for sending data from one computer to another over the network. Data is divided into small blocks and then sent out through IP packets. Since Internet links are complex, there are often multiple lines between two computers. Therefore, the router is responsible for deciding how to forward an IP packet. The characteristic of IP packets is that they are sent block by block, passing through multiple routes, but there is no guarantee of arrival, nor of arriving in order.
An IP address is actually a 32-bit integer (called IPv4). An IP address represented as a string, such as 192.168.0.1, is actually the number represented after grouping the 32-bit integer into 8-bit groups, for the purpose of easy reading.
An IPv6 address is actually a 128-bit integer. It is an upgraded version of the currently used IPv4, represented as a string similar to 2001:0db8:85a3:0042:1000:8a2e:0370:7334.
The TCP protocol is built on top of the IP protocol. The TCP protocol is responsible for establishing a reliable connection between two computers and ensuring that data packets arrive in order. The TCP protocol establishes a connection through a handshake, then numbers each IP packet to ensure that the other party receives them in order; if a packet is lost, it is automatically retransmitted.
Many commonly used higher-level protocols are built on the TCP protocol, such as the HTTP protocol used for browsers and the SMTP protocol for sending emails.
In addition to the data to be transmitted, a TCP message also contains the source IP address and destination IP address, and the source port and destination port.
What is the role of a port? When two computers communicate, sending only the IP address is not enough, because multiple network programs run on the same computer. When a TCP message arrives, whether it is handed to the browser or QQ needs the port number to distinguish. Each network program applies for a unique port number from the operating system. Thus, for two processes to establish a network connection between two computers, they each need their own IP address and their own port number.
Having understood the basic concepts of the TCP/IP protocol, and the concepts of IP addresses and ports, we can begin network programming.