CS144 Lab Assignments - 手写TCP - LAB0

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, 如下所示。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-635LLPRT-1613571152307)(https://s3.ax1x.com/2021/01/24/sbYMvD.png)]

其效果等同于

sbTh4A.png

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值