CS144笔记

概述

课程官网:CS 144: Introduction to Computer Networking

我的实现代码:zlh123123/CS144 (github.com)

Checkpoint 0 networking warmup

lab文档:check0.pdf (cs144.github.io)

1 配置实验环境

Ubuntu 22.04系统中,运行指令

1
2
sudo apt update && sudo apt install git cmake gdb build-essential clang \
clang-tidy clang-format gcc-doc pkg-config glibc-doc tcpdump tshark

当然还有多种配置环境方式,可参考check0.pdf (cs144.github.io)

2.1 客户端手动访问网站

Ubuntu命令行中输入

1
telnet cs144.keithw.org http

得到输出如下

1
2
3
Trying 104.196.238.229...
Connected to cs144.keithw.org.
Escape character is '^]'.

表明已经连接上 http://cs144.keithw.org/hello 。随后输入三行设置规则后按下回车

1
2
3
GET /hello HTTP/1.1		
Host: cs144.keithw.org
Connection: close

这里注意telnet连接是有时间限制的,太长时间不输入连接会自动关闭哦;在输入设置规则时,每条规则前不能有空格等字符,否则无法识别。

这三行代码的意思是:

  • 该请求为GET请求,使用HTTP/1.1协议,目标路径是/hello
  • 目标主机是cs144.keithw.org
  • Connection: close表示在完成请求后立即关闭与服务器的连接。

成功运行后的结果如下

1
2
3
4
5
6
7
8
9
10
11
12
HTTP/1.1 200 OK
Date: Tue, 16 Jul 2024 10:30:55 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.

这里的Hello, CS144!与直接打开网址 http://cs144.keithw.org/hello 的结果是一样的,这样就🆗啦。

这里还有一个Assignment,要求访问 http://cs144.keithw.org/lab0/sunetid ,这个sunetid似乎是学号?做法和上述一致:

1
2
3
GET /lab0/1145 HTTP/1.1
Host: cs144.keithw.org
Connection: close

输出为

1
2
3
4
5
6
7
8
9
10
11
12
HTTP/1.1 200 OK
Date: Tue, 16 Jul 2024 11:17:09 GMT
Server: Apache
X-You-Said-Your-SunetID-Was: 1145
X-Your-Code-Is: 250824
Content-length: 108
Vary: Accept-Encoding
Connection: close
Content-Type: text/plain

Hello! You told us that your SUNet ID was "1145". Please see the HTTP headers (above) for your secret code.
Connection closed by foreign host.

2.2 给自己发邮件

这个实验貌似需要斯坦福的邮箱地址才能得到效果❓这里就没有写

2.3 服务器端在干什么

首先输入指令

1
netcat -v -l -p 9090

建立一个监听端口为9090的服务器,其中

  • -v: 启用详细模式,会显示更多调试信息。
  • -l: 监听模式,告诉netcat在指定的端口上监听连接。
  • -p 9090: 指定监听的端口号为9090。

另外开一个终端,输入

1
telnet localhost 9090

使用telnet工具连接到本地主机上的端口9090。具体含义如下:

  • telnet: 是一个用于远程登录的协议和工具,通过telnet可以连接远程主机的端口。
  • localhost: 表示本地主机,即指向本机IP地址127.0.0.1。
  • 9090: 指定要连接的端口号为9090。

这个指令的作用是使用telnet工具连接到本地主机上的9090端口,来与在该端口上运行的服务进行通信或交互。

此时,向netcat窗口中输入任何信息,都会在telnet窗口中显示出来;反之亦然。

3 为TCP写一个可靠字节流传输程序

对于Internet而言,它只能“尽最大努力”来传输数据。然鹅这些数据可能会被丢失、被纂改、顺序错误或者发了多次,因此我们需要写一个程序来保证这个传输的“可靠的”。我们写的程序webget用来创建 TCP stream socket,完成上面的连接服务器和访问网页的操作。

首先下载资源文件

1
2
3
4
git clone https://github.com/cs144/minnow
cd minnow
cmake -S . -B build
cmake --build build

:这里在执行cmake指令时,可能遇到

1
2
CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
CMake 3.24.2 or higher is required. You are running version 3.22.1

在更新cmake时,可以使用下列操作

1
2
3
4
5
6
7
wget https://github.com/Kitware/CMake/releases/download/v3.25.0/cmake-3.25.0.tar.gz
tar -zxvf cmake-3.25.0.tar.gz
sudo apt-get install libssl-dev
cd cmake-3.25.0
./bootstrap
make
sudo make install

这个程序有下列要求

  • 我们要写的代码在/apps/webgets.cc中的get_URL函数
  • 要使用TCPSocket and Address这两个类
  • 代码没几行
  • 行间以\r\n进行分割

以下为编写流程