This is the 14th day of my participation in Gwen Challenge
Socket, translated as “socket,” is a convention or way for computers to communicate with each other.
Through the socket convention, one computer can receive and send data to other computers.
The typical application of socket is the Web server and browser: the browser obtains the URL entered by the user and sends a request to the server. The server analyzes the received URL and returns the corresponding Web content to the browser. The browser then parses and renders the text, picture, video and other elements to the user.
Learning socket, that is, learning how to communicate between computers, and write a practical program.
1. The IP address
Computers are distributed all over the world, and in order to communicate with them, one must know their exact location. There are many ways to determine the location of a computer, with IP addresses being the most commonly used. For example, 114.114.114.114 is the first DNS service address in the country and the third DNS service address in the world, and 127.0.0.1 is the local address.
In fact, our computer does not know the geographical location of the IP address, and when it wants to communicate, it just encapsulates the IP address in the packet to be sent, and sends it to the router for processing. The router has a very intelligent and efficient algorithm that quickly finds the target computer and delivers the packet to it, completing a one-way communication.
Port 2.
With an IP address, you can find the target computer, but you still can’t communicate. A computer can provide a variety of network services at the same time, such as Web service, FTP service (file transfer service), SMTP service (mailbox service), etc., only IP address, although the computer can correctly receive data packets, but do not know which network program to give data packets to deal with, so communication failure.
To distinguish different network programs, the computer assigns a unique Port Number to each network program. For example, Port 80 is used for the Web service, Port 21 is used for the FTP service, and Port 25 is used for the SMTP service.
A Port is a virtual, logical concept. A port can be thought of as a door through which data flows in and out. Each door has a different number, which is the port number. As shown below:
3. Agreement
A Protocol is a network communication convention that both parties must comply with to send and receive data. There are many types of protocols, such as TCP, UDP, and IP. The two parties must use the same protocol to communicate. A protocol is a specification, written by a computer organization, that specifies many details, such as how to establish connections, how to recognize each other, etc.
A protocol is merely a specification that must be implemented by computer software. For example, an IP protocol dictates how to find a target computer, so each developer must follow that protocol when developing their own software, rather than start from scratch.
4. Data transmission mode
(1) sock_stream
Connection-oriented data transfer. Data can reach another computer without error, and if damaged or lost, it can be re-sent, but more slowly.
The HTTP protocol uses SOck_stream.
(2) sock_dgram
Indicates the connectionless data transmission mode. The computer simply transmits data without checking it, and if the data is corrupted in transit or does not reach another computer, there is no way to repair it. In other words, if the data is wrong, it is wrong and cannot be retransmitted. But it’s efficient.
QQ chat uses this.
SOCK_DGRAM is not as bad as you might expect, it does not lose data frequently, and data errors are only rare events.
To sum up: The IP addresses and ports that determine which programs to communicate with across the vast Internet, the protocols and data transfer methods that dictate how data is transferred, allow two computers to communicate.