Sockets and WebSocket

Source: Internet
Author: User

I need to understand the following questions:

    1. What are the differences and connections between sockets and WebSocket?
    2. What's the relationship between WebSocket and HTML5?
    3. Do I have to use WebSocket in a browser?
    4. Can the WebSocket transmit raw data like a Socket?
    5. Is WebSocket more traffic-intensive than the Socket?

1. Overview

After choosing the WebSocket technology, inevitably, I will compare it with other protocols and techniques. The most common is the need to compare WebSocket and HTTP, Socket technology similarities and differences.

WebSocket is created to meet the growing demand for real-time communications based on the Web. In the traditional Web, in order to achieve real-time communication, the common way is to use the HTTP protocol to continuously send requests. However, this means wasting bandwidth (HTTP HEAD is relatively large) and consuming server CPU (no information is required to accept requests). (from websocket.org)

Instead of using WebSocket technology, the above-mentioned consumption is greatly reduced: (from websocket.org)

For a more detailed description, Yin Li's article is very good: WebSocket (2) – Why the WebSocket protocol was introduced.

So, what exactly is the relationship between WebSocket and the HTTP protocol? What's the connection between it and the Socket? This will refer to the OSI model and the TCP/IP protocol family.

2. OSI model and TCP/IP

The following is a description of the OSI model in Wikipedia:

Open Systems Interconnect Communication Reference Model (English: Open System Interconnection Reference Model,iso/iec 7498-1), referred to as the OSI model, a conceptual model, Presented by the International Organization for Standardization (ISO), a standard framework that attempts to interconnect computers across the world as a network.

The TCP/IP protocol can be seen as a simplification of the OSI model (the following is from Wikipedia):

It abstracts the software communication process into four abstraction layers, and adopts the way of protocol stacking, and makes different communication protocols respectively. The protocols under the set of protocols, depending on their function, are respectively attributed to the four Stratum 7, which is often considered as a simplified seven-layer OSI model.

Here is a diagram detailing the distribution of the various protocols in the TCP/IP protocol family in the OSI model, a picture wins thousands of words (from Koreb):

Here is the PDF version:

Download

The TCP/IP protocol and the content of the OSI model are numerous on the Internet. I don't need to introduce them again. Here, we just need to know that HTTP, WebSocket and other protocols are at the highest level of the OSI model: the application layer . While the IP protocol works on the network layer (layer 3rd), the TCP protocol works on the Transport Layer (layer 4th).

As for the OSI model at all levels have what system and their corresponding, here is a good article to satisfy everyone's curiosity: OSI seven layer model detailed.

3. WebSocket, HTTP, and TCP

As can be seen from the above figure, HTTP, WebSocket and other application-layer protocols are based on the TCP protocol to transfer data. We can interpret these advanced protocols as a encapsulation of TCP.

Since everyone uses the TCP protocol, then everyone's connection and disconnection should follow the three handshake and four handshake in the TCP protocol, except that the content sent after the connection is different or the time of disconnection is different.

Read more: Wireshark clutch illustration TCP Three-time handshake/four-time wave explanation

For WebSocket, it must rely on the HTTP protocol for a handshake, and after the handshake succeeds, the data is transferred directly from the TCP channel, regardless of HTTP.

4. Sockets and Webscoket

Sockets are not really a protocol. It works at the OSI model Session layer (layer 5th), which is a layer of abstraction that exists to facilitate direct use of the lower level protocol (typically TCP or UDP).

The first set of Socket APIs was Berkeley sockets, implemented in C. It is the de facto standard for sockets, POSIX sockets is built on it, and many programming languages follow this set of APIs, which can be seen in JAVA and Python.

The following excerpt is a more understandable text (from the HTTP and socket long connections and short connection differences):

A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol.

Host A's application communicates with host B's application and must establish a connection through the socket, and the socket connection must be built with the underlying TCP/IP protocol to establish a TCP connection. Establishing a TCP connection requires the underlying IP protocol to address the hosts in the network. We know that the IP protocol used by the network layer can help us to locate the target host based on the IP address, but there may be multiple applications running on one host, and how to communicate with the specified application is specified by the TCP or UPD address, or the port number. This makes it possible for a Socket instance to uniquely represent a communication link for an application on a host.

WebSocket, however, is a complete application-layer protocol that contains a set of standard APIs.

As a result, WebSocket is easier to use and the Socket is more flexible.

5. HTML5 and WebSocket

The WebSocket API is part of the HTML5 standard, but this does not mean that WebSocket must be used in HTML or only in browser-based applications.

In fact, many languages, frameworks, and servers provide WebSocket support, such as:

    • C-based libwebsocket.org
    • Socket.io based on node. js
    • Python-based Ws4py
    • websocket++ based on C + +
    • Apache support for WebSocket: Apache Module Mod_proxy_wstunnel
    • Nginx support to WebSockets: Nginx as a WebSockets Proxy, Nginx announces supports for WebSocket Protocol, WebSocket proxying
    • LIGHTTPD support for WebSocket: Mod_websocket

Sockets and WebSocket

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.