My address: blog.csdn.net/jinglijun/a…
This article we first understand the basic knowledge, so that our later study is more helpful.
Socket, WebSocket, Http, Tcp, etc. We have heard a lot about them, but we should review them when we use them.
When I studied the basics of network in college, my teacher said that network is divided into physical layer, data link layer, network layer, transmission layer, session layer, presentation layer and application layer from bottom to top. Through a preliminary understanding, I know that IP protocol corresponds to the network layer, TCP protocol corresponds to the transport layer, and HTTP protocol corresponds to the application layer. The three are essentially comparable, and socket is the encapsulation and application of TCP/IP protocol (programmer level). In other words, TPC/IP is a transport-layer protocol that deals with how data is transferred over the network, while HTTP is an application-layer protocol that deals with how data is packaged. The connection between TCP/IP and HTTP is a simple one.
“We could just use [transport layer] TCP/IP when we’re transferring data, but then without the application layer, you can’t recognize the content of the data. If you want to make sense of the data being transferred, you have to use application layer protocols, and there are many application layer protocols, like HTTP, FTP, TELNET, etc., 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.”
And we usually say the most socket is what, actually socket is the TCP/IP protocol encapsulation, socket itself is not a protocol, but a call interface (API), through socket, we can use TCP/IP protocol. In fact, sockets are not necessarily related to TCP/IP. When Socket programming interface is designed, it is hoped that it can also adapt to other network protocols. So the Socket was created to make it easier for programmers to use the TCP/IP stack. It was an abstraction of THE TCP/IP protocol and formed some of the most basic functional interfaces we know. Examples include create, Listen, connect, Accept, send, read, and write. Socket and TCP/IP protocols
“TCP/IP is just a stack of protocols. Like the operating system, it must be implemented and provided with an external interface. 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.”
The TCP/IP protocol is related, with extensive and profound I think it is not too much, just check the number of information and books on the Internet about this kind of only know, this I plan to buy some classic books (such as “TCP/IP details: Volume 1, Volume 2, volume 3) for learning, today we will first summarize some based on TCP/IP protocol application and programming interface knowledge, that is, just said a lot of HTTP and Socket.
There is a graphic description on CSDN:
HTTP is a car that provides a concrete form for encapsulating or displaying data;
Sockets are engines that provide network communication capabilities.
In fact, TCP at the transport layer is based on IP at the network layer, while HTTP at the application layer is based on TCP at the transport layer. Socket itself is not a protocol, as mentioned above, it only provides an interface for TCP or UDP programming.
Here are some important concepts that are often encountered in written tests or interviews.
What is the three-way handshake for TCP connections
First handshake: The client sends a SYN packet (SYN = J) to the server and enters the SYN_SEND state.
Second handshake: After receiving a SYN packet, the server must acknowledge the client’s SYN (ACK = J +1) and send a SYN packet (ACK = K). In this case, the server enters the SYN_RECV state.
Third handshake: After receiving the SYN+ACK packet from the server, the client sends an ACK packet (ACK = K +1) to the server. After the packet is sent, the client and the server enter the ESTABLISHED state to complete the three-way handshake.
The packet transmitted during the handshake does not contain data. After three handshakes, the client and server start data transmission. Ideally, once a TCP connection is established, it is maintained until either of the communicating parties voluntarily closes the connection. During disconnection, both the server and the client can initiate a request to disconnect the TCP connection. The disconnection process requires “four handshakes”.
Two use Socket to establish network connection steps
Establishing a Socket connection requires at least one pair of sockets, one of which runs on the client, called ClientSocket, and the other on the server, called ServerSocket.
The connection process between sockets is divided into three steps: server listening, client request, and connection confirmation.
1. Server monitoring: the server socket does not locate the specific client socket, but is in the state of waiting for the connection, monitoring the network status in real time, waiting for the connection request of the client.
2. Client request: the client socket makes a connection request, and the target connection is the server socket. To do this, the client-side socket must first describe the socket of the server to which it is connecting, specifying the address and port number of the server-side socket, and then make a connection request to the server-side socket.
3, connection confirmation: when the server socket listens to or receives the connection request of the client socket, it responds to the request of the client socket, establishes a new thread, and sends the description of the server socket to the client. Once the client confirms the description, the two sides formally establish a connection. The server socket continues to listen, receiving connection requests from other client sockets.
Features of HTTP links
HTTP, also known as Hypertext Transfer Protocol, is the foundation of Web networking and one of the protocols commonly used in mobile networking. HTTP is an application based on TCP.
The most remarkable feature of HTTP connections is that each request sent by the client requires a response from the server, and when the request is complete, the connection is released. The process from establishing a connection to closing a connection is called one connection.
4, TCP and UDP difference (test the most. I feel like)
1, TCP is link-oriented, although the insecure and unstable characteristics of the network determines how many times the handshake can not ensure the reliability of the connection, but TCP three handshake in the minimum (in fact, also to a large extent to ensure) to ensure the reliability of the connection; UDP is not connection-oriented. It does not establish a connection with the other party before transmitting data, and does not send an acknowledgement signal to the received data. The sender does not know whether the data will be correctly received and does not need to resend the data.
2. Because of the features mentioned in 1, UDP has a lower overhead and a higher data transmission rate. Because there is no need to confirm the sending and receiving data, UDP has a better real-time performance.
Know the difference between TCP and UDP, it is not hard to understand why the TCP transport protocol is slower than the transfer files using UDP QQ MSN, but doesn’t say QQ communication is not secure, because programmers can manually to validate the UDP data transceiver, such as the sender of each packet number and then validated by the receiving party ah of what, Even so, UDP achieves the transmission efficiency that TCP cannot achieve because it does not adopt the “three-way handshake” similar to TCP in the encapsulation of the underlying protocol.
Summary:
HTTP: a simple object access protocol corresponding to the application layer, HTTP is based on TCP connections.
TCP: corresponds to the transport layer.
IP protocol: corresponds to the network layer.
TCP/IP is a transport layer protocol that mainly solves how data is transmitted on the network. HTTP is an application-layer protocol that deals with how to wrap data.
The Socket is the encapsulation of TCP/IP. The Socket itself is not a protocol, but an API. Through the Socket, we can use TCP/IP.
HTTP connection: An HTTP connection is a short connection, that is, the client sends a request to the server, and the connection is broken after the server responds.
Socket connection: A Socket connection is a so-called long connection. Theoretically, once a connection is established between the client and server, it will not be actively broken. However, the connection may be disconnected due to various environmental factors, such as: the server or client host is down, the network is faulty, or there is no data transmission between the two for a long time. The network firewall may disconnect the connection to release network resources. Therefore, when there is no data transmission in a Socket connection, heartbeat messages must be sent to maintain the connection. The specific heartbeat message format is defined by the developer.
WebSocket
WebSocket is a network technology of full duplex communication between browser and server provided by HTML5. The WebSocket communication protocol was standardized by THE IETF in 2011 as RFC 6455, and the WebSocketAPI was standardized by the W3C.
In the WebSocket API, the browser and the server only need to do a handshake, and then a fast channel is formed between the browser and the server. Data can be transmitted directly between the two.
Today, many websites use polling to implement push technology. Polling is when the browser sends an HTTP request to the server at a specific interval (e.g., every 1 second), and the server returns the latest data to the client’s browser. This traditional mode has obvious disadvantages, that is, the browser needs to constantly send requests to the server. However, the HTTP request header is very long, and the data contained in it may be only a small value, which will occupy a lot of bandwidth and server resources.
The newer technique for doing polling is Comet, using AJAX. However, although this technology can achieve two-way communication, it still needs to make requests, and in Comet, long links are commonly used, which also consumes a lot of server bandwidth and resources.
Facing this situation, HTML5 defines the WebSocket protocol, which can better save server resources and bandwidth and achieve real-time communication.
Handshake protocol
In the process of implementing a Websocket connection, a Websocket connection request is made through the browser, and the server responds. This process is often called “handshaking.”
PS: Most of the later versions are functional expansions, for example, using version 7 handshake protocol also applies to version 8 handshake protocol.
Browser request
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: example.com
Origin: null
Sec-WebSocket-Key: sN9cRrP/n9NdMgdcy2VJFQ==
Sec-WebSocket-Version: 13
Server Response
HTTP / 1.1 101 Switching separate Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: fFBooB7FAkLlXgRSz0BT3v4hq5s=
Sec-WebSocket-Origin: null
Sec-WebSocket-Location: ws://example.com/
The principle of
The sec-websocket-key in the request is random, and the server uses this data to construct a summary of the SHA-1 information.
Add “sec-websocket-key” to the magic string “258eafa5-E914-47DA-95CA-C5AB0DC85b11”. Sha-1 encryption is used, base-64 encoding is performed, and the result is returned to the client as the value of the sec-websocket-Accept header.
The Websocket object on the client is bound to four events:
1. Onopen: triggered when the connection is established;
2. Onmessage: triggered when receiving a message from the server;
3, onError: connection error triggered;
4. Onclose: triggered when the connection is closed;
With these four events, it’s easy to navigate WebSocket, and it should be noted that WebSocket supports binary data transfer, so it’s much more than just a chat room application.
The WebSocket API is the next generation client-server asynchronous communication method. This communication replaces a single TCP socket using the WS or WSS protocols and can be used by arbitrary client and server programs
The great thing about the WebSocket API is that the server and client can push information to each other at any time. WebSocket is not limited to Ajax(or XHR) communication. Ajax requires the client to initiate requests, and the WebSocket server and client can push information to each other. XHR is domain bound, while WebSocket allows cross-domain communication.
In terms of servers, there are different websocket support servers on the Internet:
PHP – code.google.com/p/phpwebsoc…
Jetty – http://jetty.codehaus.org/jetty/ (version 7 to support websocket)
netty – http://www.jboss.org/netty
ruby – http://github.com/gimite/web-socket-ruby
Kaazing – http://www.kaazing.org/confluence/display/KAAZING/Home
Tomcat – http://tomcat.apache.org/ (7.0.26 support websocket)
node.js – https://github.com/Worlize/WebSocket-Node