The network is divided from bottom to top: Corresponding
Physical Layer
Data Link Layer
Network Layer (IP Protocol)
Transport Layer (TCP Protocol)
Session Layer
Application Layer and Presentation Layer (HTTP Protocol)
The socket is a wrapper and application of the TCP/IP protocol (from the programmer’s perspective). It can also be said that the TCP/IP protocol is a transport layer protocol, mainly solving how data is transmitted over the network, while HTTP is an application layer protocol, mainly solving how to package data. Regarding the relationship between TCP/IP and HTTP protocols, there is a relatively easy-to-understand introduction on the network:
“When we transmit data, we can only use the (transport layer) TCP/IP protocol, but in that case, if there is no application layer, the content of the data cannot be recognized. If we want the transmitted data to be meaningful, we must use application layer protocols, of which there are many, such as HTTP, FTP, TELNET, etc., and we can also define our own application layer protocols. The web uses the HTTP protocol as the application layer protocol to encapsulate HTTP text information, and then uses TCP/IP as the transport layer protocol to send it over the network.”
The socket we often talk about is actually a wrapper for the TCP/IP protocol. The socket itself is not a protocol, but a calling interface (API). Through the socket, we can use the TCP/IP protocol. In fact, the socket is not necessarily related to the TCP/IP protocol. When designing the socket programming interface, it was hoped that it could also adapt to other network protocols. Therefore, the emergence of the socket only makes it more convenient for programmers to use the TCP/IP protocol stack; it is an abstraction of the TCP/IP protocol, forming some of the most basic function interfaces we know, such as create, listen, connect, accept, send, read, and write, etc. There is a relatively easy-to-understand statement about the relationship between socket and TCP/IP protocol on the network:
“TCP/IP is just a protocol stack, like the operating system’s operating mechanism, it must be specifically implemented, and it must also provide external operation interfaces. Just as the operating system provides standard programming interfaces, such as the Win32 programming interface, TCP/IP must also provide interfaces for programmers to use for network development, which is the socket programming interface.”
Summarizing some knowledge based on TCP/IP protocol applications and programming interfaces, which is what we just discussed a lot about HTTP and sockets.
There is a relatively vivid description on CSDN: HTTP is a car, providing a specific form of encapsulation or display of data; the socket is the engine, providing the capability for network communication.
In fact, the transport layer TCP is based on the network layer IP protocol, while the application layer HTTP protocol is based on the transport layer TCP protocol, and the socket itself is not considered a protocol; as mentioned above, it only provides an interface for programming with TCP or UDP.
Below are some important concepts often encountered in written tests or interviews, which are specially excerpted and summarized here.
1. What is the Three-Way Handshake of TCP Connection
First Handshake: The client sends a SYN packet (SYN=j) to the server and enters the SYN_SEND state, waiting for the server’s confirmation; Second Handshake: The server receives the SYN packet and must confirm the client’s SYN (ACK=j+1), while also sending a SYN packet (SYN=k), that is, the SYN+ACK packet, at which point the server enters the SYN_RECV state; Third Handshake: The client receives the server’s SYN+ACK packet and sends an acknowledgment packet ACK (ACK=k+1) to the server. After this packet is sent, both the client and server enter the ESTABLISHED state, completing the three-way handshake. The packets transmitted during the handshake do not contain data, and only after the three-way handshake is completed do the client and server officially start transmitting data. Ideally, once a TCP connection is established, it will be maintained until either party actively closes the connection. During disconnection, both the server and client can actively initiate the request to disconnect the TCP connection, and the disconnection process requires going through a “four-way handshake” (the process is not detailed here, just that the server and client interact to ultimately determine disconnection).
2. Steps to Establish a Network Connection Using Sockets
Establishing a socket connection requires at least a pair of sockets, one running on the client side, called ClientSocket, and the other running on the server side, called ServerSocket.
The connection process between sockets is divided into three steps: server listening, client request, connection confirmation.
1. Server Listening: The server-side socket does not locate a specific client socket but is in a waiting connection state, monitoring the network status in real-time, waiting for client connection requests.
2. Client Request: This refers to the client socket making a connection request, with the target being the server-side socket. To do this, the client socket must first describe the server socket it wants to connect to, indicating the address and port number of the server socket, and then it makes a connection request to the server socket.
3. Connection Confirmation: When the server-side socket listens to or receives the connection request from the client socket, it responds to the client’s request, establishes a new thread, and sends the server socket description to the client. Once the client confirms this description, the two sides officially establish a connection. Meanwhile, the server-side socket continues to listen, receiving connection requests from other client sockets.
3. Characteristics of HTTP Links
The HTTP protocol, or Hypertext Transfer Protocol, is the foundation of web networking and is also one of the commonly used protocols for mobile networking. The HTTP protocol is an application built on top of the TCP protocol.
The most notable feature of an HTTP connection is that each request sent by the client requires a response from the server, and after the request ends, the connection is actively released. The process from establishing a connection to closing it is called “one connection.”
4. Differences Between TCP and UDP
1. TCP is connection-oriented. Although the insecure and unstable nature of the network determines that no number of handshakes can guarantee the reliability of the connection, the three-way handshake of TCP at least guarantees the reliability of the connection to a minimum extent (in fact, it guarantees it to a large extent). In contrast, UDP is connectionless; it does not establish a connection with the other party before transmitting data, nor does it send acknowledgment signals for received data. The sender does not know whether the data will be received correctly, and naturally does not need to retransmit, so UDP is a connectionless and unreliable data transmission protocol.
2. Due to the characteristics mentioned in 1, UDP has lower overhead and higher data transmission rates, as it does not need to confirm the sending and receiving of data, thus providing better real-time performance.
Knowing the differences between TCP and UDP makes it easy to understand why the file transfer speed of MSN, which uses the TCP transmission protocol, is slower than that of QQ, which uses UDP. However, this does not mean that QQ’s communication is insecure, as programmers can manually verify the sending and receiving of UDP data, such as numbering each data packet by the sender and having the receiver verify it. Even so, UDP achieves transmission efficiency that TCP cannot reach because it does not use a “three-way handshake” like TCP in its underlying protocol encapsulation.
Java Developer Group【Click to Join】