Do you know how to use a TCP connection HTTP is it? Today I'll tell you!

1, HTTP is how to use the TCP connection;

Almost all HTTP traffic is carried by the world of TCP / IP, TCP / IP layered protocol network is a common set of packet-switched networks and global computer equipment in use. The client application can open a TCP / IP connection, connect to the server application may run anywhere in the world. Once the connection is established, between the client and server computers exchange packets will never be lost, damaged or out of order.

Although the message will not be lost or damaged, but if the computer crashes or the network communication between the client and the server will still be disconnected. In this case, it will notify the client and server communication is interrupted.

When the browser receives a URL of time, it will perform several steps corresponding to the following

  1. Browser parses out the hostname;

  2. The browser queries the IP address of the host name;

  3. Browser obtain the port number;

  4. The browser initiates a link to the IP address corresponding to the port number;

  5. The browser sends to the server a HTTP GET message;

  6. HTTP browser reads the corresponding message from the server;

  7. The browser closes the connection;

Do you know how to use a TCP connection HTTP is it?  Today I'll tell you!

1.1, a basic knowledge of TCP connections

TCP is a reliable data pipeline

Do you know how to use a TCP connection HTTP is it?  Today I'll tell you!

TCP will be sequential, error-free data bearer HTTP, TCP provides a reliable transport pipeline bits to HTTP. Connecting one end of TCP bytes filled from the original order will be properly transmitted from the other end.

TCP flow is segmented by the IP packet transfer

TCP data is transmitted by a small data blocks called IP packets (or IP datagram) is.

Do you know how to use a TCP connection HTTP is it?  Today I'll tell you!

In this case, as HTTP is the most top-level "HTTP over TCP over IP" this "stack" in the. HTTPS secure version which is inserted between the HTTP and TCP layer password encryption (security layer), that is, a right half of the figure (referred to as the SSL or TLS).

When an HTTP message to be transmitted, it will be in the form of content stream data packets transmitted in order of a TCP connection opened through. After receipt of TCP data stream, the data stream will be cut into small data blocks are referred to as segments, and the segment encapsulated in an IP packet for transmission, as we see in FIG contents via the Internet:

Do you know how to use a TCP connection HTTP is it?  Today I'll tell you!

每个 TCP 段都是由 IP 分组承载,从一个 IP 地址发送到另一个 IP 地址的。

而每个 IP 分组中都包括:

  • 一个 IP 分组首部(通常为 20 字节);

  • 一个 TCP 段首部(通常为 20 字节);

  • 一个 TCP 数据块(0 个或多个字节)。

IP 首部包含了源和目的 IP 地址、长度和其他一些标记。TCP 段的首部包含了 TCP 端口 号、TCP 控制标记,以及用于数据排序和完整性检查的一些数字值。

保持 TCP 连接的持续不间断地运行

在任意时刻计算机都可以有几条 TCP 连接处于打开状态。TCP 是通过端口号来保持所有 这些连接的正确运行的。端口号和雇员使用的电话分机号很类似。

这就和我之前举得例子是一样的,公司的总机和你自己的座机一样,公司的总机号码能将你接到前台,而分机号 可以将你接到正确的雇员位置一样,IP 地址可以将你连接到正确的计算机,而端口号则 可以将你连接到正确的应用程序上去。TCP 连接是通过 4 个值来识别的:

源IP 地址源端口号目的IP 地址目的端口号

这 4 个值一起唯一地定义了一条连接。两条不同的 TCP 连接不能拥有 4 个完全相同的地 址组件值(但不同连接的部分组件可以拥有相同的值)。

这里需要我们注意的是,有些连接共享了相同的目的端口号,有些连接使用了相同的源 IP 地址,有些使用了相同的目的 IP 地址,但没有两个不同连接所有的 4 个值都一样。

TCP 套接字

操作系统提供了一些操纵其 TCP 连接的工具。为了更具体地说明问题,我们来看一个 TCP 编程接口,这些套接字我就不一一介绍了,我给大家一个表格,大家可以理解一下

套接字API调用 描 述
s = socket() 创建一个新的、未命名、未关联的套接字
bind(s,) 向套接字赋一个本地端口号和接口
connect(s,) 创建一条连接本地套接字与远程主机及端口的连接
listen(s,...) 标识一个本地套接字,使其可以合法接受连接
s2 = accept(s) 等待某人建立一条到本地端口的连接

套接字 API 允许用户创建 TCP 的端点数据结构,将这些端点与远程服务器的 TCP 端点进 行连接,并对数据流进行读写。TCP API 隐藏了所有底层网络协议的握手细节,以及 TCP 数据流与 IP 分组之间的分段和重装细节。

TCP 客户端和服务器是如何通过 TCP 套接字接口进行通信的

Do you know how to use a TCP connection HTTP is it?  Today I'll tell you!

上图中说明了可以怎样通过套接字 API 来凸显客户端和服务器在实现 HTTP 事务时所应执行的步骤。

2、TCP 连接的握手

TCP 连接握手需要经过以下几个步骤。如图所示:

Do you know how to use a TCP connection HTTP is it?  Today I'll tell you!

请求新的 TCP 连接时,客户端要向服务器发送一个小的 TCP 分组(通常是 40 ~ 60 个字节)。这个分组中设置了一个特殊的 SYN 标记,说明这是一个连接请求。

  1. 如果服务器接受了连接,就会对一些连接参数进行计算,并向客户端回送一个 TCP 分组,这个分组中的 SYN 和 ACK 标记都被置位,说明连接请求已被接受。

  2. 最后,客户端向服务器回送一条确认信息,通知它连接已成功建立

我们永远不会看到这些分组——这些分组都由 TCP/IP 软件管理,对其是不可见 的。HTTP 程序员看到的只是创建 TCP 连接时存在的时延。

在这里我们需要注意的就是 TCP 连接的握手时延,通常 HTTP 事务都不会交换太多数据,此时,SYN/SYN+ACK 握手(参见图中的 a 段 和图中的 b 段)会产生一个可测量的时延。TCP 连接的 ACK 分组(参见图中的 c 段)通常都足够大,可以承载整个 HTTP 请求报文,而且很多 HTTP 服务器响应报文都可 以放入一个 IP 分组 中去(比如,响应是包含了装饰性图片的小型 HTML 文件,或者是对浏览器高速缓存请求产生的 304 Not Modified 响应)。

TCP 慢启动

TCP 数据传输的性能还取决于 TCP 连接的使用期(age)。TCP 连接会随着时间进行自 我“调谐”,起初会限制连接的最大速度,如果数据成功传输,会随着时间的推移提高传输 的速度。这种调谐被称为 TCP 慢启动(slow start),用于防止因特网的突然过载和拥 塞。

TCP 慢启动限制了一个 TCP 端点在任意时刻可以传输的分组数。简单来说,每成功接收 一个分组,发送端就有了发送另外两个分组的权限。如果某个 HTTP 事务有大量数据要发 送,是不能一次将所有分组都发送出去的。必须发送一个分组,等待确认;然后可以发送 两个分组,每个分组都必须被确认,这样就可以发送四个分组了,以此类推。这种方式被 称为“打开拥塞窗口”。

由于存在这种拥塞控制特性,所以新连接的传输速度会比已经交换过一定量数据的、“已 调谐”连接慢一些。由于已调谐连接要更快一些,所以 HTTP 中有一些可以重用现存连接 的工具。

3、HTTP 连接的处理

前面我们说了 TCP 连接,我们重新来分析一下 HTTP ,之前我也说过在 HTTP 1.0的时候和1.1之后,有 Keep-Alive ,关于 Keep-Alive 不懂的请翻看前面的公众号的文章内容,接下来我分几个内容给大家讲述 HTTP 对连接上的处理。

  • 并行连接:通过多条 TCP 连接发起并发的 HTTP 请求。

  • 持久连接:重用 TCP 连接,以消除连接及关闭时延。

  • 管道化连接:通过共享的 TCP 连接发起并发的 HTTP 请求。

我们来看一下串行:

Do you know how to use a TCP connection HTTP is it?  Today I'll tell you!

每个事务都需要(串行地建立)一条 新的连接,那么连接时延和慢启动时延就会叠加起来

并行连接就是说 HTTP 允许客户端打开多条连接,并行的去执行多个 HTTP 的事务,就会出现多条线路平行的情况。

Do you know how to use a TCP connection HTTP is it?  Today I'll tell you!

其实并行连接并没有说是页面的传输速度,是因为多个对象同时在进展,所以,他的速度要比叠加起来,让你在感觉上快不少。

持久连接

HTTP 1.1 allows HTTP transaction apparatus after the end of the TCP connection in an open state, in order to request reuse of existing connections for future HTTP. After the end of the transaction remains in a state of open TCP connection is called a persistent connection. Non-persistent connection will be closed after the end of each transaction. Persistent connection remains open between the different transactions until the client or the server decided to shut up.

Piped connection (it was also known pipelined)

HTTP / 1.1 allows the optional use of a persistent connection request pipe. It is a further performance optimization with respect to the connection keep-alive. Before the response arrives, the request may be placed in a plurality of queues. When the other end of the first flow earth request server via a network, the second and the third request may be transmitted starts. Under conditions of high latency networks, this can reduce the round trip time of the network, to improve performance.

In fact, pipelining it means the transfer process to respond to them without having to wait for the server, and then made a few, the browser HTTP request bulk submission can significantly reduce page load times, especially in the transmission delay (lag / latency) than the case is high (e.g., satellite link). The key to this technique is that a plurality of HTTP request message may be simultaneously inserted into a TCP packet, the packet can only be submitted at the same time issuing a plurality of request packets thereby reduce excess load on the network and reduce the line.

Guess you like

Origin blog.51cto.com/14230003/2454565