Differences between http and socket
TCP/IP is a transport layer protocol, mainly used for data transmission over the network. http is an application layer protocol used to encapsulate data. only the TCP/IP protocol (Transport Layer) is used for data transmission ). without the application layer to identify data content, the transmitted protocol is useless.
There are many application layer protocols, such as FTP, HTTP, and TELNET. You can customize the application layer protocol by yourself. the WEB uses HTTP as the transport layer protocol. to encapsulate HTTP text information, and then use TCP/IP as the transport layer protocol to send data to the network.
Socket is the encapsulation of TCP/IP protocol. Socket is only an interface, not a protocol. Only through Socket can we use TCP/IP protocol.
Of course, in addition to TCP, UDP can also be used to transmit data.
3. Differences between HTTP and Socket connections
1 TCP connection
To understand Socket, you must understand TCP connections.
"Three-way handshake" for establishing a TCP connection ":
First time: the client sends a SYN Packet (syn = j) to the server and is in the SYN_SEND status.
Second: After receiving the SYN packet, the server must confirm the customer's SYN (syn = j + 1) and send a SYN Packet (syn = k ), that is, the SYN + ACK package. At this time, the server enters the SYN_RECV status.
The third time: when the client receives the SYN + ACK packet from the server, it sends SYN (syn = k + 1) to the server. After the packet is sent, both the server and client enter the ESTABLISHED status. complete three handshakes.
Data is not transmitted during handshaking. after the handshake, the server and the client start to transmit data. Ideally, once a TCP connection is established, the TCP connection will continue until either of the two parties actively disconnects the connection.
2. HTTP Connection
The most notable feature of HTTP: the client sends requests to the server. after the request ends, the link is released. HTTP is a short connection. the common practice is to send "keep connection" requests to the server at intervals without any data. this ensures that the client is "online" on the server.
3 socket Principle
A Socket connection requires at least one Socket, which can be divided into clientSocket and serverSocket. The connection can be divided into three steps:
Server listening: the server does not locate the socket of a specific client, but is always in the listening status.
Client request: the socket of the client needs to describe the socket of the server to which it connects. Provide the address and port number, and then send a connection request to the server socket.
Connection Confirmation: when the server socket receives a request from the client socket, it responds to the request from the client socket, creates a new thread, and sends the description of the socket on the server to the client, once the client confirms this description, the connection is established. the server socket continues to be in the listening status and continues to receive connection requests from other client sockets.
4 Socket connection and TCP Connection
When creating a Socket connection, you can specify the transport layer protocol. It can be TCP or UDP. When using a TCP connection, the Socket is a TCP connection.
5 Socket connection and HTTP Connection
Generally, a Socket connection is a TCP connection. Once a Socket connection is established, both parties start to send data content to each other until both parties disconnect the connection. in practical applications, due to too many network nodes, the nodes will be disconnected during transmission. therefore, the node is active by polling the high-speed network.
The HTTP connection uses the "request-response" method, which not only establishes a connection during the request, but also returns data only after the client requests the server.
In many cases, the server needs to actively push data to the client to ensure real-time synchronization between the client and the server. if both parties are connected by a Socket, the server can directly send data to the client. if both parties are connected via HTTP, the server must wait for the client to send the request before returning the data to the client. therefore, the client sends requests to the server periodically, not only to stay online, but also to check whether the server has new data. If so, the client transmits the data.
Http is a short connection, similar to the clicking of the request information on a common webpage. The security is high, and the socket is a persistent connection. After the connection, as long as the two parties do not close the connection, the connection will always exist and the security is low. Each user needs short connections without frequent operations.
Socket Application scenarios: online games, bank interaction, and payment.
Http application scenarios: Company OA services and Internet services.