CS 144: Introduction to Computer Networking, Fall 2020
https://cs144.github.io/My Repo
https://github.com/wine99/cs144-20fa
LAB0 在 master 分支,LAB1 - 7 在对应名字的分支。
思否主页:https://segmentfault.com/u/wine99
webget
What is webget?
参照 lab0.pdf 2.1 Fetch a Web page, 如下所示。
其效果等同于
Write Webget
参考 API 文档 https://cs144.github.io/doc/lab0/class_t_c_p_socket.html。
注意 lab0.pdf 中的几点提示:
- Please note that in HTTP, each line must be ended with “\r\n” (it’s not sufficient to use just “\n” or endl).
- Don’t forget to include the “Connection: close” line in your client’s request. This tells the server that it shouldn’t wait around for your client to send any more requests after this one. Instead, the server will send one reply and then will immediately end its outgoing bytestream (the one from the server’s socket to your socket). You’ll discover that your incoming byte stream has ended because your socket will reach “EOF” (end of file) when you have read the entire byte stream coming from the server. That’s how your client will know that the server has finished its reply.
- Make sure to read and print all the output from the server until the socket reaches “EOF” (end of file) — a single call to read is not enough.
在 GET 请求中写明 Connection: close
可以让服务器马上进入连接释放的过程(发一个 FIN 过来),我们在发送完 GET 同样需要 shutdown(SHUT_WR)
,进入连接释放过程(发一个 FIN 过去)。(不太理解的话,写完 Lab4,就能理解了。)
void get_URL(const string &host, const string &path) {
// Your code here.
// You will need to connect to the "http" service on
// the computer whose name is in the "host" string,
// then request the URL path given in the "path" string.
// Then you'll need to print out everything the server sends back,
// (not just one call to read() -- everything) until you reach
// the "eof" (end of file).
// cerr << "Function called: get_URL(" << host << ", " << path << ").\n";
// cerr << "Warning: get_URL() has not been implemented yet.\n";
TCPSocket sock1;
sock1.connect(Address(host, "http"));
sock1.write("GET " + path + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
while (!sock1