首页 > 其他分享 >Stanford CS 144, Lab 0: networking warmup 实验

Stanford CS 144, Lab 0: networking warmup 实验

时间:2023-03-29 15:37:57浏览次数:52  
标签:144 networking warmup stream buffer len HTTP byte const

Stanford CS 144, Lab 0: networking warmup

目录

>>> lsb_release -a // 运行环境展示
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04 LTS
Release:        22.04
Codename:       jammy

>>> g++ -v
gcc version 8.4.0 (Ubuntu 8.4.0-3ubuntu2) 

2 Networking by hand

2.1 Fetch a Web page

visit http://cs144.keithw.org/hello and observe the result:

observeWebPage

Now, we want to do same things by our hand.

  1. telnet cs144.keithw.org httpThis tells the telnet program to open a reliable byte stream between your computer and another computer (named \(\texttt{cs144.keithw.org}\)), and with a particular service running on that computer: the “http” service, for the Hyper-Text Transfer Protocol, used by the World Wide Web. Then, i saw the output in the terminal:

    >>> user$ telnet cs144.keithw.org http
    Trying 104.196.238.229...
    Connected to cs144.keithw.org.
    Escape character is '^]'.
    Connection closed by foreign host.
    

    Telnet是一种网络协议,用于远程登录到计算机或其他设备上并在其上执行命令。通过Telnet,用户可以通过网络连接到远程主机并像本地主机一样进行命令行操作。在Linux系统中,Telnet客户端程序和服务器程序可以使用telnet命令来启动和连接。

    Telnet协议使用客户端/服务器模型。Telnet客户端向Telnet服务器建立连接并提供凭据以进行身份验证。一旦连接建立,Telnet客户端可以像使用本地终端一样在远程系统上运行命令。

    尽管Telnet协议在过去很受欢迎,但由于其不安全性,现在已被SSH协议取代。因为Telnet协议在传输数据时未加密,所以可能会泄露用户的敏感信息(例如用户名和密码)。SSH协议提供了加密和身份验证功能,可以更安全地远程连接到Linux系统。

  2. Type GET /hello HTTP/1.1 ⏎

  3. Type Host: cs144.keithw.org ⏎

  4. Type Connection: close ⏎

  5. Hit the Enter key one more times: This sends an empty line and tells the server that you are done with your HTTP request.

  6. If all went well, you will see the same response that your browser saw, preceded by HTTP headers that tell the browser how to interpret(explain) the response.

>>> user$ telnet cs144.keithw.org http
Trying 104.196.238.229...
Connected to cs144.keithw.org.
Escape character is '^]'.
GET /hello HTTP/1.1
Host: cs144.keithw.org
Connection: close

HTTP/1.1 200 OK
Date: Mon, 20 Mar 2023 11:23:14 GMT
Server: Apache
Last-Modified: Thu, 13 Dec 2018 15:45:29 GMT
ETag: "e-57ce93446cb64"
Accept-Ranges: bytes
Content-Length: 14
Connection: close
Content-Type: text/plain

Hello, CS144!
Connection closed by foreign host.

Then, we'll explain the meaning of each step.

GET /hello HTTP/1.1 This tells the server the path part of the URL(The starting with the third slash, like: http://cs144.keithw.org/hello.)

这段命令是 HTTP 协议中客户端向服务器发送 HTTP 请求的一部分,它由三部分组成:

  1. 请求方法(Request Method):在这里是 GET。它指定了客户端请求的动作类型,常见的方法有 GET、POST、PUT、DELETE 等。
  2. 请求 URI(Uniform Resource Identifier):在这里是 /hello。它指定了客户端要请求的资源的位置,URI 由路径和查询参数组成。例如,在这个例子中,URI 是 /hello,表示客户端请求位于服务器根目录下的名为 hello 的资源。
  3. 协议版本(Protocol Version):在这里是 HTTP/1.1。它指定了客户端使用的 HTTP 协议版本。在 HTTP/1.1 中,客户端和服务器之间的通信是持久连接的,这意味着客户端可以在同一连接上发送多个请求,并且服务器可以在同一连接上返回多个响应。

因此,GET /hello HTTP/1.1 这个命令的意思是客户端使用 HTTP/1.1 协议,向服务器发送一个 GET 请求,请求服务器位于根目录下的名为 hello 的资源。服务器在收到请求后将返回相应的响应,包括状态码、头部信息和响应内容等。

Host: cs144.keithw.org This tells the server the host part of the URL. (The part between http:// and the third slash.)

这段命令是HTTP请求中的一个头部信息(header),用于指定客户端请求的目标服务器。

在这个例子中,Host: cs144.keithw.org 指定了客户端要请求的服务器主机名为 cs144.keithw.org。HTTP/1.1 引入了“虚拟主机”(Virtual Host)的概念,使得多个域名可以共享同一个IP地址,并根据 Host 头部信息将请求路由到正确的服务器。因此,Host 头部信息对于客户端请求的处理非常重要。

除了 Host 头部信息,HTTP请求还可以包含许多其他头部信息,用于传递关于客户端、请求内容、请求处理方式和请求接受格式等方面的信息。这些头部信息通常使用“键值对”的形式表示,例如“Content-Type: application/json”表示请求中包含的数据类型为JSON格式。

Connection: close This tells the server that you are finished making requests, and it should close the connection as soon as if finishes replying.

这段命令是HTTP请求中的一个头部信息(header),用于指定客户端和服务器之间的连接类型。

在这个例子中,Connection: close 指定了客户端和服务器之间的连接类型为“关闭连接”。这意味着,在客户端发送完请求并收到服务器的响应后,连接将被立即关闭,而不是保持打开状态以等待其他请求。这种连接类型称为“短连接”(short-lived connection)。

在HTTP/1.1中,默认情况下,客户端和服务器之间的连接类型为“持久连接”(persistent connection),也称为“长连接”(long-lived connection)。这意味着客户端可以在同一连接上发送多个请求,并且服务器可以在同一连接上返回多个响应。在这种情况下,Connection头部信息应设置为“Connection: keep-alive”。

在HTTP/2中,连接类型默认为“持久连接”,而不需要显式指定 Connection头部信息。

因此,Connection头部信息用于指定客户端和服务器之间的连接类型,通常包括“关闭连接”和“保持连接”两种类型。它对于HTTP请求和响应的处理和性能优化非常重要。

Assignment:

>>> user$ telnet cs144.keithw.org http
Trying 104.196.238.229...
Connected to cs144.keithw.org.
Escape character is '^]'.
GET /lab0/sunetid HTTP/1.1
Host: cs144.keithw.org
Connection: close

HTTP/1.1 200 OK
Date: Mon, 20 Mar 2023 11:59:46 GMT
Server: Apache
X-You-Said-Your-SunetID-Was: sunetid
X-Your-Code-Is: 746452
Content-length: 111
Vary: Accept-Encoding
Connection: close
Content-Type: text/plain

Hello! You told us that your SUNet ID was "sunetid". Please see the HTTP headers (above) for your secret code.

2.2 Send yourself an email

Now that you know how to fetch a Web page, it’s time to send an email message, again using a reliable byte stream to a service running on another computer. (Since we don't have a Stanford email, we have to use our own, such as QQ email.)

QQ 邮箱授权码 tuplkrnwplxtbage

由于我们没有 Stanford 的邮箱

标签:144,networking,warmup,stream,buffer,len,HTTP,byte,const
From: https://www.cnblogs.com/Last--Whisper/p/17269081.html

相关文章