The difference between WebSocket and sockets

Source: Internet
Author: User

Some time ago wrote two articles about HTTP and WebSocket, and some people said they wanted to know the difference between websocket and sockets. This question before also has thought, oneself to this is has the approximate answer, but is not quite certain, therefore collects some information (actually is all sorts of Google), has looked many previous documents, thought some story is very interesting, the collation follows, is a rumor.

The pictures are all from Google image search, such as invasion and deletion.

Short answer

Like Java and JavaScript, there's no big relationship, but it doesn't really matter. So to speak:

    • In terms of naming, socket is a popular concept, websocket borrowed this concept;
    • Using aspects, totally two things.

The relationship between Java and JavaScript long answer

What do we want to explore when we explore the differences and linkages of two things?

For me, most of the situation is to know the two things themselves, not just want to understand the "difference" itself. Then the most direct solution to this problem should be to understand the socket and the source and usage of websocket, then their differences and connections are self-evident.

Socket

Sockets can have a lot of meaning, and it is more relevant to the original intention is to refer to an end-to-end connection, the two ends are called Sockets. For it practitioners, it often refers to two connections in a TCP/IP network environment, and most API providers (such as operating systems, JDK) often provide interfaces based on this concept, so it is often a programming concept for developers. At the same time, inter-process communication in the operating system also has the concept of socket, but this socket is not based on the network Transport Layer protocol.

Sockets in Unix

The concept of using sockets in the operating system is also used for interprocess communication, which is very similar to the TCP/IP-based socket concept, which represents the two parties that transmit data in the operating system, except that it is no longer based on the network protocol, but rather the filesystem of the operating system itself.

Sockets in the Network

Commonly referred to as the socket API, refers to the operating system (or may not be the operating system) provided for the Transport Layer (TCP/UDP) abstraction of the interface. The current socket API generally follows the BSD socket specification (including Windows). It is said that the specification is actually not very accurate, the specification is POSIX, but BSD Unix in the implementation of the socket is widely used, so became the actual specification. If you are using HTTP to build a service, then you don't need to care about the socket, if you want to build services based on TCP/IP, then the socket is probably the API you'll be exposed to.


Location of HTTP in a TCP/IP network

As you can see, HTTP is based on the transport layer of the TCP protocol, and the socket API is also, so just from the use, can think of the socket and HTTP similar (but one is a written Internet protocol, one is always used as a programming concept), the Transport layer protocol is another Direct use, because according to the design, the network to the user interface should be in the application layer.

The origin of the socket name

Like many other things on the internet, the socket is named after the famous ARPANET (Advanced Study Projects Agency), where the socket in the early arpanet refers to a source or destination address-- This is roughly what we call the IP address and port number today. At the earliest, a socket refers to a 40-bit number (described in RFC33, but in RFC36 does not explicitly use 40-bit numbers to identify an address), where the first 32 points to the address (socket number, Roughly equivalent to IP), the latter 8 bits are the source of the data sent (link, roughly equivalent to the port number). There are many versions of what they are called, which are not strictly listed here.

Unofficial history of the port number

With the development of ARPANET, later (rfc433,socket number List) Socket numbers are explicitly defined as a 40-bit figure, where the last 8 bits are used to make a particular application use (e.g. 1 is telnet). This 8-digit number has many names: link, socket name, AEN (another eight number, see this name I am also drunk), engineers tease up also very hard.

The word "port number" was actually used in the specification of the internet later. As for why the port number is 16-bit, I think there are two reasons, one is for the engineers at that time, if each port number to identify a program, 65,535 port number is almost enough. Two may be to align, ^_^!!。

The original meaning of the socket

The socket used in the history mentioned above, including the socket used in the TCP document, actually refers to one end of the network transmission and is a concept of virtualization.

WebSocket

The above simply describes the meaning of the socket, because of the age, many things can not be so clear. But WebSocket is a very recent thing that allows us to see how it has become the way we see it now.

WHATWG (Web Hypertext Application Technology working Group)

Stories about HTML5 Many people know that the World Wide Web has given up on HTML, and then a group of people (and those who say they work for them) have created the WHATWG organization to drive the continued development of the HTML language, while They have also developed a number of technical standards on the web that are constantly being accepted by the authorities. WebSocket is part of the Web application published by WHATWG (that is, HTML5).

Why would there be websocket?

In about 08, the WG engineers needed a full-duplex connection in their discussion of the network environment, just beginning to be called "tcpconnection", and discussed the features that the protocol needed to support, roughly the same as the websocket we saw today. They believe that some technologies, such as long polling, Comet, based on existing HTTP, are not enough to satisfy this requirement, and it is necessary to define a completely new protocol.

The origin of the name

In a lot of documents about HTML5 or websocket, you can see a name, Hixie (Ian Hickson), a spokesperson for WHATWG, who worked for Netscape, Opera, and Google, A company that looks at work knows the background of this person.


Hixie

June 18, 08, a group of WHATWG engineers discussing some technical issues, an engineer mentioned that "we discussed before the thing, do not call tcpconnection, or an individual name", followed by a few names are mentioned, Duplexconnection, Tcpsocket,socketconnection, an engineer named Mcarter (Michael Carter), said he was going to write an article about comet, and if he could determine the name, he would cite the name in the article.

The socket has been used by people to represent both ends of a connection in the network, considering how to make it easier for the engineer to accept it, and later Hixie said, "I see websocket that's the right name (Hixie briefly pops back online to record That "WebSocket" would probably is a good new name for the Tcpconnection object) "and everyone has no objection, and then Mcarter published the article in Comet daily Inde Pendence day:html5 WebSocket liberates Comet from Hacks, later with the support of the major browsers to WebSocket, it became the actual standard, the IETF also used this name.

Below is the definition of the WebSocket interface in the WHATWG document

Enum Binarytype {"Blob","Arraybuffer"}; [Constructor (usvstring URL, optional (domstring or sequence<domstring>) protocols = []), exposed= (Window,worker)] Interface Websocket:eventtarget {readonly attribute usvstring URL;Ready stateConstUnsignedShort connecting =0;ConstUnsignedShort OPEN =1;ConstUnsignedShort CLOSING =2;ConstUnsignedShort CLOSED =3; ReadOnly attributeUnsignedShort readyState; ReadOnly attributeUnsignedLongLong Bufferedamount;Networking attribute EventHandler OnOpen; Attribute EventHandler onerror; Attribute EventHandler OnClose; readonly attribute domstring extensions; ReadOnly attribute domstring Protocol;voidClose([Clamp] optional unsigned short code, optional usvstring reason); //Messaging attribute EventHandler onmessage; attribute Binarytype binarytype; void Send(usvstring data); void Send(Blob data); void Send(ArrayBuffer data); void Send(Arraybufferview data);};            
Determination of content

The emergence of most new technologies is based on the existing technology to pave the way, websocket content to determine the same, there is comet can not see the contribution of comet is a very interesting technology, is interested to see here

Conclusion

Can think of websocket as http,http and socket what relationship, websocket and socket is what relationship.

Inquisitive HTTP and WebSocket Protocol (i)
Inquisitive HTTP and WebSocket Protocol (ii)




Original link: http://www.jianshu.com/p/59b5594ffbb0

The difference between WebSocket and sockets

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.