Difference Http, Socket, WebSocket connections and differences between HTTP and SOCKET of


the difference between the socket and http:
the Http protocols: Simple Object Access Protocol, corresponding to the application layer. Http protocol is based on TCP links.
tcp Protocol: transport layer corresponds to the
ip protocol: The network layer corresponds to
TCP / IP transport layer protocol, the main address how data transmission in the network; and Http is an application layer protocol, address how the main pack data.

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

Http connection: http connection is known as a short connection, and the client sends a request to the server, the server-side connection that is appropriate will be cut off.

socket connection: socket connection in a timely manner so-called long connection, in theory, the client and server Once the connection is established, it will not take the initiative to cut off; but due to various environmental factors may be disconnected, for example: the server or client host down a long time there is no data transmission between a network failure, or both, network firewall may disconnect the link freed network resources. So when a socket connection is not transmitting data, the need for a continuous connection location heartbeat message is sent, the specific heartbeat message format is defined by the developer's own.


---------------------
Disclaimer: This article is CSDN blogger "weixin_41648325 'original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/weixin_41648325/article/details/79412158

Long connection 1.HTTP generally stuck with one minute only, and is determined by the browser, your page is difficult to control this behavior.
Socket connections can be maintained for a long time, days, months are likely, as long as the network continues, the program does not end, and can be flexibly programmed control.
2.HTTP connection is based on the Socket connection. In actual network stack, Socket connector indeed part of HTTP connections. However, viewed from the HTTP protocol, it generally refers to that portion of the connection itself.

TCP / IP protocol stack is divided into four layers: the application layer, transport layer, network layer, data link layer,

Each layer has a corresponding protocol, following FIG.

IP:

Network layer protocol; (highway)

TCP and UDP:

Transport layer protocol; (truck)

HTTP:

Application layer protocol; (cargo). HTTP (hypertext transfer protocol) TCP protocol to transmit information between the two computers (typically a Web server and client) is utilized. Clients use a Web browser sends an HTTP request to the Web server, Web server sends the requested information to the client.

SOCKET:

Sockets, TCP / IP network API. (Ports / station) and the intermediate layer is the Socket application software TCP / IP protocol suite to communicate abstraction layer, which is a set of interfaces. is a socket abstraction layer between the application layer and the transport layer, it is the TCP / IP layer complex operations few simple abstract interface layer serves the calling process has been implemented in a communication network.

TCP/IP:

Representative Transmission Control Protocol / Internet Protocol, refers to a series of protocols, TCP / IP on the basis of a simplified model of the OSI model, into four layers, from bottom to top are: a network interface layer, network layer, transport layer, application layer. Comparative OSI architecture are as follows:

TCP / UDP difference:

TCP

(Transmission Control Protocol, Transmission Control Protocol) :( similar call)

Connection-oriented, reliable transport (to ensure the correctness of the data), ordered (sequential ensure data), transmission of large amounts of data (stream mode), slow, multi-system resource requirements, the program structure is more complex,

Each TCP connection can only point to point,

TCP header overhead of 20 bytes.

UDP

(User Datagram Protocol, User Data Protocol) :( similar text messaging)

面向非连接 、传输不可靠(可能丢包)、无序、传输少量数据(数据报模式)、速度快,对系统资源的要求少,程序结构较简单 ,

UDP支持一对一,一对多,多对一和多对多的交互通信,

UDP的首部开销小,只有8个字节。

tcp三次握手建立连接:

第一次握手:客户端发送syn包(seq=x)到服务器,并进入SYN_SEND状态,等待服务器确认;

第二次握手:服务器收到syn包,必须确认客户的SYN(ack=x+1),同时自己也发送一个SYN包(seq=y),即SYN+ACK包,此时服务器进入SYN_RECV状态;

第三次握手:客户端收到服务器的SYN+ACK包,向服务器发送确认包ACK(ack=y+1),此包发送完毕,客户端和服务器进入ESTABLISHED状态,完成三次握手。

握手过程中传送的包里不包含数据,三次握手完毕后,客户端与服务器才正式开始传送数据。理想状态下,TCP连接一旦建立,在通信双方中的任何一方主动关闭连接之前,TCP 连接都将被一直保持下去。

主机A向主机B发出连接请求数据包:“我想给你发数据,可以吗?”,这是第一次对话;

主机B向主机A发送同意连接和要求同步(同步就是两台主机一个在发送,一个在接收,协调工作)的数据包:“可以,你什么时候发?”,这是第二次对话;

主机A再发出一个数据包确认主机B的要求同步:“我现在就发,你接着吧!”,这是第三次对话。

三次“对话”的目的是使数据包的发送和接收同步,经过三次“对话”之后,主机A才向主机B正式发送数据。

Websocket

Websocket协议解决了服务器与客户端全双工通信的问题。

注:什么是单工、半双工、全工通信?

信息只能单向传送为单工;

信息能双向传送但不能同时双向传送称为半双工;

信息能够同时双向传送则称为全双工。

websocket协议解析

wensocket协议包含两部分:一部分是“握手”,一部分是“数据传输”。

WebSocket和Socket区别

可以把WebSocket想象成HTTP(应用层),HTTP和Socket什么关系,WebSocket和Socket就是什么关系。

HTTP 协议有一个缺陷:通信只能由客户端发起,做不到服务器主动向客户端推送信息。

WebSocket 协议在2008年诞生,2011年成为国际标准。所有浏览器都已经支持了。

它的最大特点就是,服务器可以主动向客户端推送信息,客户端也可以主动向服务器发送信息,是真正的双向平等对话,属于服务器推送技术的一种

 

 

 

 

要弄明白 http 和 socket 首先要熟悉网络七层:物 数 网 传 会 表 应,如图1

如图1

HTTP 协议:超文本传输协议,对应于应用层,用于如何封装数据.

TCP/UDP 协议:传输控制协议,对应于传输层,主要解决数据在网络中的传输。

IP 协议:对应于网络层,同样解决数据在网络中的传输。

传输数据的时候只使用 TCP/IP 协议(传输层),如果没有应用层来识别数据内容,传输后的协议都是无用的。

应用层协议很多 FTP,HTTP,TELNET等,可以自己定义应用层协议。

web 使用 HTTP 作传输层协议,以封装 HTTP 文本信息,然后使用 TCP/IP 做传输层协议,将数据发送到网络上。

 

一、HTTP 协议

http 为短连接:客户端发送请求都需要服务器端回送响应.请求结束后,主动释放链接,因此为短连接。通常的做法是,不需要任何数据,也要保持每隔一段时间向服务器发送"保持连接"的请求。这样可以保证客户端在服务器端是"上线"状态。

HTTP连接使用的是"请求-响应"方式,不仅在请求时建立连接,而且客户端向服务器端请求后,服务器才返回数据。

 

二、Socket 连接

要想明白 Socket,必须要理解 TCP 连接。

TCP 三次握手:握手过程中并不传输数据,在握手后服务器与客户端才开始传输数据,理想状态下,TCP 连接一旦建立,在通讯双方中的任何一方主动断开连接之前 TCP 连接会一直保持下去。

Socket 是对 TCP/IP 协议的封装,Socket 只是个接口不是协议,通过 Socket 我们才能使用 TCP/IP 协议,除了 TCP,也可以使用 UDP 协议来传递数据。

创建 Socket 连接的时候,可以指定传输层协议,可以是 TCP 或者 UDP,当用 TCP 连接,该Socket就是个TCP连接,反之。

Socket 原理

Socket 连接,至少需要一对套接字,分为 clientSocket,serverSocket 连接分为3个步骤:

(1) 服务器监听:服务器并不定位具体客户端的套接字,而是时刻处于监听状态;

(2) 客户端请求:客户端的套接字要描述它要连接的服务器的套接字,提供地址和端口号,然后向服务器套接字提出连接请求;

(3) 连接确认:当服务器套接字收到客户端套接字发来的请求后,就响应客户端套接字的请求,并建立一个新的线程,把服务器端的套接字的描述发给客户端。一旦客户端确认了此描述,就正式建立连接。而服务器套接字继续处于监听状态,继续接收其他客户端套接字的连接请求.

Socket为长连接:通常情况下Socket 连接就是 TCP 连接,因此 Socket 连接一旦建立,通讯双方开始互发数据内容,直到双方断开连接。在实际应用中,由于网络节点过多,在传输过程中,会被节点断开连接,因此要通过轮询高速网络,该节点处于活跃状态。

 

很多情况下,都是需要服务器端向客户端主动推送数据,保持客户端与服务端的实时同步。

若双方是 Socket 连接,可以由服务器直接向客户端发送数据。

若双方是 HTTP 连接,则服务器需要等客户端发送请求后,才能将数据回传给客户端。

因此,客户端定时向服务器端发送请求,不仅可以保持在线,同时也询问服务器是否有新数据,如果有就将数据传给客户端。

 

 

参考:HTTP 和 SOCKET 的区别

参考:Http、Socket、WebSocket之间联系与区别 

参考:Socket和Http之间的区别和概述

Guess you like

Origin www.cnblogs.com/aspirant/p/11334957.html