WebSocket (reprint)

Source: Internet
Author: User
Tags ack to domain

In this article we first understand the basic knowledge, so that we can learn more useful behind.

Socket,websocket,http,tcp and so on we have heard the ear has a cocoon, but the use of time to review it.

University Learning Network Foundation when the teacher said, the network from the bottom up into the physical layer, Data link layer, network layer, transport layer, Session layer, presentation layer and application layer. Through the preliminary understanding, I know that the IP protocol corresponds to the network layer, the TCP protocol corresponds to the transport layer, and the HTTP protocol corresponds to the application layer, the three are not inherently comparable, the socket is the TCP/IP protocol encapsulation and application (programmer level). It can also be said that the TPC/IP protocol is the Transport Layer protocol, mainly to solve how the data in the network transmission, and HTTP is the application layer protocol, mainly to solve how to package the data. Regarding the relationship between TCP/IP and HTTP protocol, the network has a relatively easy to understand introduction:

"When we transmit data, we can only use the (Transport Layer) TCP/IP protocol, but in that case, if there is no application layer, it will not be able to identify the data content, if you want to make the transferred data meaningful, you must use the Application layer protocol, the application layer protocol, such as HTTP, FTP, Telnet, etc., You can also define the application layer protocol yourself. The web uses the HTTP protocol as an application-layer protocol to encapsulate HTTP text information and then send it to the network using TCP/IP as the Transport layer protocol. ”

And we usually say the most socket is what, in fact, the socket is the TCP/IP protocol encapsulation, the socket itself is not a protocol, but a call interface (API), through the socket, we can use the TCP/IP protocol. In fact, the socket is not necessarily associated with the TCP/IP protocol. The socket programming interface is designed to adapt to other network protocols as well. So, the advent of sockets just makes it easier for programmers to use the TCP/IP protocol stack, which is an abstraction of the TCP/IP protocol, thus forming some of the most basic function interfaces we know, such as Create, listen, connect, accept, send, Read and write, and so on. The network has a section on the socket and TCP/IP protocol relationship is relatively easy to understand:

"TCP/IP is just a stack of protocols, just like operating systems, which must be implemented in a specific way, as well as providing an external interface for operations." This is like the operating system will provide a standard programming interface, such as the Win32 programming interface, TCP/IP should also provide the interface for programmers to do network development, this is the socket programming interface. ”

About the TCP/IP protocol only, with the broad and profound I think not too, just check the online about this kind of information and books only the number of documents to know, this I intend to buy some classic books (such as "TCP/IP Detailed: Volume One, Volume II, Volume three") to learn, Today, we summarize some of the knowledge based on TCP/IP protocol-based applications and programming interfaces, which is just a lot of HTTP and sockets.

There is a csdn description of the image:

HTTP is a sedan that provides a specific form of encapsulation or display data;

The socket is the engine that provides the ability to communicate over the network.

In fact, the transport layer TCP is based on the network layer of IP protocol, and the application layer of the HTTP protocol is based on the transport layer of the TCP protocol, and the socket itself is not a protocol, as mentioned above, it just provides a TCP or UDP programming interface.

Here are some of the important concepts that are often encountered in a written test or interview and are summarized here.

What is a three-time handshake for a 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 to confirm;
Second handshake: The server receives the SYN packet, it must confirm the customer's SYN (ACK=J+1), and also send itself a SYN packet (syn=k), that is, the Syn+ack packet, when the server enters the SYN_RECV state;
Third handshake: The client receives the server's Syn+ack packet, sends the acknowledgment packet ack (ACK=K+1) to the server, the packet is sent, the client and the server enter the established state, and the handshake is completed three times.
The data is not included in the packets that are delivered during the handshake, and the client and server formally begin transmitting the data after the three handshake is complete. Ideally, once a TCP connection is established, the TCP connection is maintained until either side of the communication actively closes the connection. When disconnected, both the server and the client can initiate a request to disconnect the TCP connection, and the disconnection process requires a "four handshake" (the process is not fine-grained, that is, the server interacts with the client and finally determines the disconnection)

Two steps to establish a network connection using a socket

Establishing a socket connection requires at least one pair of sockets, one running on the client, called Clientsocket, and the other running on the server side, called ServerSocket.

The connection between sockets is divided into three steps: Server listening, client request, connection acknowledgement.

1, Server monitoring: Server-side sockets do not locate the specific client sockets, but in the status of waiting for the connection, real-time monitoring network status, waiting for the client connection request.

2, client request: Refers to the client socket to make a connection request, to connect to the target is the server-side socket. To do this, the client's socket must first describe the socket of the server it is connecting to, indicate 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 side socket is heard or received the connection request of the client socket, it responds to the request of the client socket, establishes a new thread, sends the description of the server end socket to the client, and once the client confirms the description, the two sides formally establish the connection. While the server-side socket continues to be in the listening state, it continues to receive connection requests from other client sockets.

Features of the three HTTP links

The HTTP protocol, the Hypertext Transfer Protocol (hypertext Transfer Protocol), is the foundation of Web networking and one of the most commonly used protocols for mobile networking, an application built on the TCP protocol.

The most notable feature of an HTTP connection is that each request sent by the client requires a server loopback response, and the connection is actively released after the request has ended. The process from establishing a connection to closing a connection is called a "one-time connection."

Four, the difference between TCP and UDP (the most test). I'm getting a bad test.

1, TCP is link-oriented, although the network's insecure instability determines how many times the handshake can not guarantee the reliability of the connection, but the TCP three handshake at a minimum (in fact, to a large extent guaranteed) to ensure the reliability of the connection, and UDP is not a connection-oriented, UDP transmission data before the connection with the other party, the data received is not sent to confirm the signal, the sender does not know whether the data will be properly received, of course, there is no need to resend, so that UDP is a non-connected, unreliable data transmission protocol.

2, also due to 1 of the characteristics of the said, so that the cost of UDP smaller data transmission rate is higher, because there is no need to send and receive data confirmation, so UDP real-time better.

Know the difference between TCP and UDP, it is not difficult to understand why the use of TCP transmission protocol MSN than UDP transmission file slow, but can not say that QQ communication is not safe, because the programmer can manually verify the data sent and received by UDP, For example, the sender of each packet number and then by the receiver to verify AH what, even so, UDP because the package on the underlying protocol does not adopt a TCP-like "three-time handshake" to achieve the TCP can not achieve the transmission efficiency.

Simple summary:

HTTP protocol: Simple Object Access Protocol, corresponding to the application layer, the HTTP protocol is based on the TCP connection.

TCP protocol: Corresponds to the transport layer.

IP protocol: Corresponds to the network layer.


TCP/IP is a Transport layer protocol that mainly addresses how data travels across the network, while HTTP is an application-layer protocol that addresses how data is packaged.

Socket is the TCP/IP protocol encapsulation, the socket itself is not a protocol, but a call interface (API), through the socket, we can use the TCP/IP protocol.



HTTP connection: HTTP connection is the so-called short connection, that is, the client sends a request to the server side, the server-side response after the connection will be broken;

Socket connection: Socket is the so-called long connection, in theory, the client and server side once established connection will not be active disconnection, but due to various environmental factors may be disconnected, such as: Server or client host down, network failure, Or there is no data transfer between the two for a long time, the network firewall may disconnect the connection to free up network resources. So when there is no data transmission in a socket connection, it is necessary to send a heartbeat message in order to maintain the connection ~ ~ The specific heartbeat message format is defined by the developer itself.



WebSocket

WebSocket is a network technology that HTML5 started to provide full duplex communication between the browser and the server. The WebSocket communication Agreement was established as the standard RFC 6455,websocketapi by the IETF in 2011.

In the WebSocket API, browsers and servers only need to act as a handshake, and then a fast channel is formed between the browser and the server. The data can be transmitted to each other directly between the two.

Today, many Web sites are polled for the technology they use to implement push technology. Polling is at a specific time interval (such as every 1 seconds), the browser sends an HTTP request to the server, and then the server returns the latest data to the client's browser. This traditional pattern brings with it the obvious drawback that the browser needs to constantly make requests to the server, but the header of the HTTP request is very long, and the data contained in it may be a small value, which consumes a lot of bandwidth and server resources.

and the newer technology to do the polling effect is comet, using Ajax. However, although this technology can achieve two-way communication, but still need to make requests, and in comet, the widespread use of long links, which also consumes a lot of server bandwidth and resources.

In the face of this situation, HTML5 defines the WebSocket protocol, which can better save the server resources and bandwidth and achieve real-time communication.

Handshake Protocol

During the implementation of the WebSocket connection, a WebSocket connection request is made via the browser, and then the server responds, which is often referred to as a "handshake" (handshaking).
PS: Later versions are mostly functional extensions, such as the use of the 7th version of the handshake Protocol also applies to the 8th version of the 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 protocols

Upgrade:websocket

Connection:upgrade

sec-websocket-accept:ffboob7fakllxgrsz0bt3v4hq5s=

Sec-websocket-origin:null

sec-websocket-location:ws://example.com/

Principle

In the request "Sec-websocket-key" is random, the server side will use this data to construct a SHA-1 information digest.

Add "Sec-websocket-key" to a magical string "258eafa5-e914-47da-95ca-c5ab0dc85b11". Use SHA-1 encryption, then BASE-64 encode the result as the "sec-websocket-accept" header value, returned to the client.

The client's WebSocket object is bound to a total of four events:

1, OnOpen: Trigger when connection is established;

2, OnMessage: Trigger when receiving message from server;

3, OnError: Trigger when connection error occurs;

4, OnClose: Trigger When the connection is closed;

With these 4 events, we can easily and easily navigate websocket, and it's important to note that WebSocket supports the transfer of binary data, so it's much more than just a chat room app.

The WebSocket API is the next generation of client-server asynchronous communication methods. This communication replaces a single TCP socket that uses the WS or WSS protocol and can be used with any client and server program

The great thing about the WebSocket API is that servers and clients can push information to each other at any point in time. WebSocket is not limited to communicating in Ajax (or XHR), Ajax technology requires client-initiated requests, while WebSocket servers and clients can push information to each other, xhr are subject to domain restrictions, and websocket allows cross-domain communication.

On the server side, there are different servers for WebSocket support on the Internet:

php-http://code.google.com/p/phpwebsocket/

jetty-http://jetty.codehaus.org/jetty/(version 7 started 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

WebSocket (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.