本文最后更新于 749 天前,其中的信息可能已经有所发展或是发生改变。
Preparations
Virtual machine setup instructions
推荐使用:vscode + ssh 连接虚拟机
Lab Checkpoint 0: networking warmup
2.1 Fetch a Web page
$ 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: Fri, 28 Oct 2022 16:55:15 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.
2.3 Listening and connecting
可能出现下面的错误:
cs144@cs144vm:~/sponge/build$ netcat -v -l -p 9090
netcat: getnameinfo: Temporary failure in name resolution
The problem could be with the DNS resolver. Try updating the namespace in
/etc/resolv.conf
as follows.
nameserver 8.8.8.8
3.4 Writing webget
void get_URL(const string &host, const string &path) {
// Your code here.
// initialize variables
TCPSocket Tsocket;
Address addr = Address(host, "http");
Tsocket.connect(addr);
// Write a string, possibly blocking until all is written
string msg = "GET " + path + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n";
Tsocket.write(msg);
// socket.h -> SHUT_RD/WR/RDWR,先用SHUT_WR关闭写,避免服务器等待
Tsocket.shutdown(SHUT_WR);
while(!Tsocket.eof()){
cout << Tsocket.read();
}
Tsocket.close();
}