​The difference among HTTP, WebSocket and Socket.

For these three concepts, many people do not know them clearly. Here as below we give a simple introduce, and then you can search more details on the internet by yourself. 
1, HTTP and WebSocket each is an application layer protocol, TCP/IP is a network layer and transport layer protocol, Socket is a TCP/IP protocol abstraction layer API package. 
2, HTTP-get, post requests and WebSocket communication will always go through the TCP/IP layer. 
3, The transmitted data can be anything when using directly Socket API to get connection and to transmit data, for example, when sending HTTP protocol GET requests,  you will receive a bunch of strings whose content is http protocol-related request string if the server uses a listening socket API.
4, In the same way, the WebSocket also transmits date through TCP/IP, but there are some differences during establishing connection of WebSocket .
5.Webstocket establish the connection by handshake with the server via http protocol first, then the sever resolve the appropriate protocol, confirming whether the connection is HTTP or WebSocket. If it is WebSocket connection, double-ended directly access to the TCP/IP regular three-way handshake stage, and then the connection have established , starting two-way date communication , the same as Socket communication.
6, As described on above, when using WebSocket to do long connection and conduct two-way communication, the sever needs to resolve the WebSocket agreement to establish a connection on the browser. If only using Socket
API to receive date, then it needs to resolve the WebSocket agreement by itself.
7, Nowadays there are WebSocket third-party libraries by different kinds of languages , such as libwebsocket.org based on C, Socket.io Based on the Node. Js, WebSocket++Based on the c + + , ws4py Based on the Python and also Apache, Nginx, lighttpd has a corresponding WebSocket library.


To reply to the article, please Login or registered