"The web is how to connect the" study notes a

Embedded Software Engineer recent interview, hr will ask to network programming this one, especially for the TCP / IP protocol, TCP three-way handshake, waving four times, the difference between UDP and TCP, the difference in the transmission process HTTP, HTTP get and post in the , HTTPS encryption method, Linux created under TCP, acceptance and so on, due to the knowledge that a blogger network is very weak, it suffered a major loss, decided to take the past few days cramming about network knowledge, look at the book of network programming, do down notes.

"The web is how to connect the" study notes a

Q: We usually how the Internet is it?

First, we enter the URL in the browser, the browser will generate a URL request message according to the meaning, after the request message is generated, the operating system will be commissioned browser sends a request to the web server

1.1 URL

There are several commonly used URL (web address), http: visit web server, ftp: ftp server access

 

After the completion of the web browser parses URL, you know where to access the target, the next, the web browser will use the HTTP protocol to access the web server

 

1.2HTTP agreement

HTTP protocol defines the message content and the step of interaction between the client and the server

HTTP process: the client sends a request message to the server, the message including the URI and method, URI indicates that the file name stored web page data, methods represent how to make web server to complete the work. After the web server receives the request message, the URI complete their work and methods, then the result is stored in a response message and sends back to the client, at the beginning of the respective message contains a status code indicating an execution result success or failure of the operation. The client receives, the browser will read out the required data from the message and displayed on the screen

1.2.1 HTTP two common methods

Methods Table 1-1 lists the most commonly used one is the GET method. Generally, when we get access to the web server web page data, is using the GET method. The so-called general access process is probably this: First, in the request message written on the GET method, and then write the data page is stored in the URI filename "dir / file.html", which means that we need to get the dir / file data .html file. When the web server receives the message, opens dir / file.html inside the file and read data, and then store the read data in response to the message, and returns it to the client. Finally, the client browser receives the data and displayed on the screen.

还有一个经常使用的方法就是POST 。我们在表单中填写数据并将其发送给web服务器时就会使用这个方法。当我们在网上商城填写收货地址和姓名,或者是在网上填写问卷时,都会遇到带有输入框的网页,而这些可以输入信息的部分就是表单。使用POST方法时,URI会指向web服务器中运行的一个应用程序2'的文件名,典型的例子包括"index.cgi"Indexphp等。然后,在请求消息中,除了方法和U心之外,还要加上传递给应用程序和脚本的数据。这里的数据也就是用户在输人框里填写的信息。当服务器收到消息后,web服务器会将请求消息中的数据发送给URI指定的应用程序。最后,web服务器从应用程序接收输出的结果,会将它存放到响应消息中并返回给客户端。

1.3 生成HTTP请求消息

回到浏览器中来,对URL解析后,确定了web服务器和文件名,接下来根据这些生成请求消息

1.4  发送请求后会收到响应

响应消息的格式和请求消息基本是相同的,差别只在于第一行上,在响应消息中,第一行的内容为状态码和响应短语,用来表示请求执行的结果是成功还是出错,状态码和响应短语表示的内容相同,但他们的用途不同,状态码是一个数字,它主要用来向程序告知执行的结果,相对地,响应短语则是一段文字,用来向人们告知执行的结果。

返回响应消息后,浏览器会将数据提取出来显示在屏幕上,如果网页的内容有图片的话,也将图片的文件名写进URI生成请求消息,由于每条请求消息只能写一个URI,所以每次只能获取一个文件,如果有3张图片,那么获取网页加上获取图片,一共要向web服务器发送4个请求消息,总结起来就是:1 条请求消息中只能写1个URI。如果需要获取多个文件,必须对每个文件单独发送1条请求。

 

1.5 向DNS服务器查询web服务器的IP地址

尽管浏览器能够解析URL生成请求消息,但它本身不具备将消息发送到网络中的功能,所以,需要委托操作系统来实现。在委托之前,还需要先查询web服务器的IP地址,这样操作系统才知道要发送的地址在哪里。

关于IP地址的划分在此就不再解释。

注意:主机号部分的比特全部为0 或者全部为1 时代表两种特殊的含义。主机号部分全部为0 代表整个子网而不是子网中的某台设备。此外,主机号部分全部为1代表向子网上所有设备发送包,即广播。

什么是DNS呢?

我们要发送请求消息前,必须知道web服务器的IP地址,有的小伙伴会说,为什么不直接用域名作为它的地址呢,想想,IP地址4个字节,域名至少也要几十到几百个字节,这增加了路由器的负担,传送数据也会花费更大的时间对吧。所以我们现在使用的方案是人来使用域名,路由器使用IP地址,为了填补两者之间的障碍,需要一个机制,能通过域名查询IP地址或通过IP地址查询域名,这个机制就叫做DNS域名解析服务。

我们的计算机上一定有相应的DNS客户端,简称DNS解析器,解析器实际上是一段程序,它包含在操作系统的socket库中。Socket 库是用于调用网络功能的程序组件集合。调用解析器后,解析器会向DNS 服务器发送查询消息,然后DNS 服务器会返回响应消息。响应消息中包含查询到的IP 地址,解析器会取出IP地址,并将其写入浏览器指定的内存地址中接下来,浏览器在向Web 服务器发送消息时,只要从该内存地址取出IP 地址,并将它与HTTP 请求消息一起交给操作系统就可以了。总结:根据域名查询IP 地址时,浏览器会使用Socket 库中的解析器。

今天弄懂了HTTP协议的交互过程,以及DNS的基本原理,下节将继续学习DNS的工作过程以及如何委托协议栈发送消息

转载请注明作者、出处,谢谢

 

Guess you like

Origin www.cnblogs.com/cyyz-le/p/11203900.html