The difference between Socket TCP HTTP

Socket

Socket programming link

Socket

Socket is the cornerstone of communication and the basic operation unit of network communication supporting TCP/IP protocol. It is an abstract representation of the endpoint in the network communication process, including five kinds of information necessary for network communication: the protocol used for the connection, the IP address of the local host, the protocol port of the local process, the IP address of the remote host, and the protocol of the remote process port.

When the application layer communicates data through the transport layer, TCP will encounter the problem of providing concurrent services for multiple application processes at the same time. Multiple TCP connections or multiple application processes may need to
transmit data through the same TCP protocol port. In order to distinguish between different application program processes and connections, many computer operating systems provide a socket (Socket) interface for the application program to interact with the TCP/IP protocol. The application
layer can distinguish the communication from different application processes or network connections through the Socket interface with the transport layer, and realize the concurrent service of data transmission.

Socket establishment

Socket connection requires at least a pair of sockets, one of which runs on the client side, called ClientSocket, and the other runs on the server side, called ServerSocket.

The connection process between sockets is divided into three steps: server monitoring, client request, and connection confirmation.

  • Server monitoring: The server-side socket does not locate a specific client socket, but is in a state of waiting for connection, monitoring the network status in real time, and waiting for the client's connection request.
  • Client request: Refers to the client's socket to make a connection request, and the target to be connected is the server-side socket. For this reason, the socket of the client must first describe the socket of the server it wants to connect to, point out the address and port number of the server-side socket, and then make a connection request to the server-side socket.
  • Connection confirmation: When the server-side socket monitors or receives the connection request of the client-side socket, it responds to the client-side socket request, establishes a new thread, and sends the description of the server-side socket To the client, once the client confirms this description, the two parties will formally establish a connection. The server socket continues to be in the listening state and continues to receive connection requests from other client sockets.

TCP

The mobile phone can use the networking function because the bottom layer of the mobile phone implements the TCP/IP protocol, which enables the mobile terminal to establish a TCP connection through the wireless network. The TCP protocol can provide an interface to the upper-layer network, so that the upper-layer network data transmission is established on the "undifferentiated" network

Explanation of three handshake and four wave of hands

HTTP

The HTTP protocol is the Hypertext Transfer Protocol (Hypertext Transfer Protocol), which is the basis of Web networking and one of the commonly used protocols for mobile phone networking. The HTTP protocol is an application built on top of the TCP protocol.

The most notable feature of HTTP connection is that every request sent by the client requires the server to send back a response, and after the request is over, the connection is actively released. The process from establishing a connection to closing a connection is called "a connection".

  • In HTTP 1.0, each client request requires a separate connection to be established, and the connection is automatically released after the request is processed.
  • In HTTP 1.1, multiple requests can be processed in one connection, and multiple requests can be overlapped, and there is no need to wait for one request to end before sending the next request.

Since HTTP will actively release the connection after each request, HTTP connection is a kind of "short connection". To keep the client program online, you need to continuously initiate connection
requests to the server . The usual practice is not necessary to obtain any real-time data, the client also maintains a fixed period of time to send a request to "stay connected" to the server every server back to the client after receiving the request is
complex, indicating that the client knows "Online". If the server fails to receive the client's request for a long time, the client is considered "offline"; if the client fails to receive the server's reply for a long time, the network is considered disconnected.

Socket connection TCP/IP

When creating a Socket connection, you can specify the transport layer protocol used. Socket can support different transport layer protocols (TCP or UDP). When the TCP protocol is used to connect, the Socket connection is a TCP connection.

Socket is the encapsulation and application of the TCP/IP protocol (at the programmer level). It can also be said that the TPC/IP protocol is a transport layer protocol, which mainly solves how data is transmitted in the network, while HTTP is an application layer protocol, which mainly solves how to package data. Regarding the relationship between TCP/IP and HTTP, the network has a relatively easy to understand introduction:

When transmitting data, you can only use the (transport layer) TCP/IP protocol , but in that case, if there is no application layer, the data content cannot be recognized. If you want to make the transmitted data meaningful, you must use the application layer protocol . There are many application layer protocols, such as HTTP, FTP, TELNET, etc. You can also define application layer protocols yourself. WEB uses HTTP protocol as application layer protocol to encapsulate HTTP text information, and then uses TCP/IP as transport layer protocol to send it to the network.

What is a socket ? In fact, a socket is an encapsulation of the TCP/IP protocol. Socket itself is not a protocol, but a call interface (API). Only through Socket can we use the TCP/IP protocol . In fact, Socket is not necessarily related to the TCP/IP protocol. When the Socket programming interface was designed, it was hoped that it could also adapt to other network protocols. Therefore, the emergence of Socket only makes it more convenient for programmers to use the TCP/IP protocol stack. It is an abstraction of the TCP/IP protocol and forms some of the most basic functional interfaces we know, such as create, listen, connect, accept, send, read, write, etc. The network has a statement about the relationship between socket and TCP/IP protocol that is easier to understand:
"TCP/IP is just a protocol stack, just like the operating mechanism of an operating system, it must be implemented in detail, and an external operation interface must also be provided. This Just like the operating system will provide a standard programming interface, such as the win32 programming interface, TCP/IP must also provide an interface for programmers to do network development. This is the Socket programming interface."

In fact, the TCP of the transport layer is based on the IP protocol of the network layer, and the HTTP protocol of the application layer is based on the TCP protocol of the transport layer. Socket itself is not a protocol , it just provides a programming for TCP or UDP. interface. Socket is a tool for port communication development, and it needs to be more low-level.

Socket connection HTTP

Since the Socket connection is usually a TCP connection, once the Socket connection is established, the two communicating parties can start to send data to each other until the connection between the two parties is disconnected. However, in actual network applications, the communication between the client and the server often needs to traverse multiple intermediate nodes, such as routers, gateways, firewalls, etc. Most firewalls will close inactive connections for a long time by default, causing the Socket connection to be broken. Therefore, it is necessary to inform the network through polling that the connection is active.

The HTTP connection uses the "request-response" method. Not only the connection needs to be established when the request is made, but also after the client sends a request to the server, the server can reply to the data.

In many cases, the server needs to actively push data to the client to keep the client and server data in real time and synchronized. At this time, if the two parties establish a Socket connection, the server can directly transmit the data to the client; if the two parties establish an HTTP connection, the server needs to wait until the client sends a request before sending the data back to the client. Therefore, The client periodically sends a connection request to the server, which can not only stay online, but also "ask" whether the server has new data, and if so, it will send the data to the client.
The http protocol is the agreement of the application layer

Guess you like

Origin blog.csdn.net/weixin_45824920/article/details/115308381