What is a Socket?
- TPC/IP protocol is a transport layer protocol, which mainly solves how data is transmitted in the network.
- Socket is the encapsulation and application of TCP/IP protocol (programmer level);
- HTTP is an application-layer protocol that deals with how to wrap data.
The relationship between TCP/IP and HTTP is this: “We can just use [transport layer] TCP/IP when we transfer data, but then without the application layer, the content of the data cannot be recognized. Application layer protocols must be used if you want to make sense of the data being transferred. There are many application-layer protocols, such as HTTP, FTP, and TELNET. You can also define application-layer protocols. The WEB uses HTTP as an application-layer protocol to encapsulate HTTP text, and then uses TCP/IP as a transport-layer protocol to send it to the network.”
What is a Socket? In fact, a Socket is the encapsulation of TCP/IP. The Socket itself is not a protocol, but an API. Through sockets, we can use TCP/IP protocol. The relationship between sockets and TCP/IP is as follows: TCP/IP is just a protocol stack, just like the operating mechanism of an operating system. It must be implemented in detail and also provide external operation interfaces. Just as the operating system provides a standard programming interface, such as the Win32 programming interface, TCP/IP also provides an interface for programmers to do network development, which is the Socket programming interface.”
As you can see from the figure above, HTTP is based on TCP at the transport layer, and Socket API is also based on TCP at the transport layer, so in terms of usage, Socket and HTTP can be considered similar (but one is a written Internet protocol, the other is a programming concept that has been used for a long time), which is another direct use of the transport layer protocol. Because by design, the network’s interface to the user should be at the application layer.
Source of WebSocket protocol:
WebSocket is part of the Web Application published by the WHATWG (i.e., HTML5). Around 2008, WG engineers were talking about the need for a full-duplex connection in a network environment, initially called “TCPConnection”, and discussed what the protocol would need to support, which would be similar to the WebSocket we see today. They decided that some of the technologies (such as long polling and Comet) based on existing HTTP did not meet this requirement and needed to define an entirely new protocol. The WebSocket protocol is designed to solve the problem of multiple HTTP requests and long rotations when browser-based applications need to pull resources… And created by.
What is the difference between WebSocket and Socket?
They have nothing to do with each other, just like Lei Feng and Lei Feng tower.
Why use Websocket?
HTTP1.1 uses persistent Connection by default. Multiple Request/Response pairs can be transmitted over a TCP connection. But the basic HTTP model is still one Request for one Response. In two-way communication (the client needs to send data to the server, and the server also needs to send information to the client in real time, a chat system is a typical two-way communication), the following solutions are generally used:
- Polling (polling), which causes a waste of resources on both the network and communication, and is not real-time.
- Long polling, the client sends a Request with a long timeout, and the server holds the connection and returns a Response when new data arrives, which consumes less network bandwidth than #1, and other similar things.
- Long connection, in fact, some people are confused about the concept of long connection, I am talking about HTTP long connection (1). If you use a Socket to set up a TCP long connection (2), then the long connection (2) is the same as WebSocket. In fact, TCP long connections are the basis of WebSockets, but if you use HTTP long connections, In essence, it is still a Request/Response message pair, which still causes problems such as waste of resources and poor real-time performance.
Websocket protocol contents:
WebSocket is intended to replace THE use of HTTP in two-way communication scenarios, and some implementations are also based on HTTP (the default ports of WS are 80 and 443). Existing network environments (clients, servers, network middlemen, proxies, and so on) have good support for HTTP, so doing this takes full advantage of the existing HTTP infrastructure and is somewhat backward compatible. The WS protocol consists of two parts: handshake and data transmission.
Shake hands
It is implemented using Http. The client initiates a handshake request using HTTP. After receiving the request, the server switches the TCP protocol in use from HTTP to WebSocket.
The handshake request header takes the Upgrade parameter to Upgrade the protocol type:
Upgrade: Upgrade is the header field used in HTTP1.1 to define the transformation protocol. It says that if the server supports it, the client wants to switch to another “application layer” (WebSocket) protocol using the “connection” already established at the existing “network layer” (TCP connection in this case).
Upgrade extension: Upgrade is the header field used for protocol Upgrade in HTTP. In the extended protocol content, there are more ways to initiate protocol transformation initiated by the client, and the server can choose not to accept the protocol Upgrade request from the client. The server can also initiate a protocol upgrade.
Request URI format:
ws-URI = "ws:" "/ /" host [ ":" port ] path [ "?" query ]
wss-URI = "wss:" "/ /" host [ ":" port ] path [ "?"Query] host = <host, defined in [RFC3986], Section 3.2.2> port = <port, defined in [RFC3986], Section 3.2.3> path = <path-abempty, defined in [RFC3986], Section 3.3> query = <query, defined in [RFC3986], Section 3.4 >Copy the code
The data transfer
After receiving the handshake request, the server replies with a response message. Once the handshake reply is sent, the server considers that the WebSocket connection has been successfully established and is in the OPEN state. It can start sending data.
All data sent in WebSocket is sent in the form of frames. All data frames sent by the client must be masked, but all data frames sent by the server cannot be masked. Otherwise, the other party needs to send a close frame.
A frame contains a frame type identifier, a load length, and a load. The payload includes both extension content and application content.
WebSocket vs. HTTP
The same
- Both are application layer protocols based on TCP.
- Request/Response model is used to establish the connection.
- Errors are handled the same way during connection establishment, and WS may return the same return code as HTTP at this stage.
- Can transfer data over the network.
The difference between
- WS uses HTTP to establish connections, but defines a new set of header fields that are not used in HTTP.
- A WS connection cannot be forwarded through a middleman; it must be a direct connection.
- After the WS connection is established, both communicating parties can send data to the other party at any time.
- After the WS connection is established, the data is transferred using frames and the Request message is no longer required.
- WS data frames are ordered.
Reference: www.jianshu.com/p/59b5594ff…