博客
关于我
CS144 计算机网络实验lab1环境搭建以及实验过程(测试百分百通过)
阅读量:292 次
发布时间:2019-03-01

本文共 4025 字,大约阅读时间需要 13 分钟。

环境搭建

Vmware虚拟机

Ubuntu20.04

执行官方提供的shell脚本下载环境依赖

vscode remote

 

先看主函数

#include "socket.hh"#include "util.hh"#include 
#include
using namespace std;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";}int main(int argc, char *argv[]) { try { if (argc <= 0) { abort(); // For sticklers: don't try to access argv[0] if argc <= 0. } // The program takes two command-line arguments: the hostname and "path" part of the URL. // Print the usage message unless there are these two arguments (plus the program name // itself, so arg count = 3 in total). if (argc != 3) { cerr << "Usage: " << argv[0] << " HOST PATH\n"; cerr << "\tExample: " << argv[0] << " stanford.edu /class/cs144\n"; return EXIT_FAILURE; } // Get the command-line arguments. const string host = argv[1]; const string path = argv[2]; // Call the student-written function. get_URL(host, path); } catch (const exception &e) { cerr << e.what() << "\n"; return EXIT_FAILURE; } return EXIT_SUCCESS;}

 这个内容是用C++实现一个获取http服务器数据的简单客户端程序。回顾一下用原始API写一个客户端的过程,以及简单的HTTP协议头的写法。

这里已经封装好了相关的类,在TcpSocket中,阅读源码调用即可。

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).    // Address address(host);    TCPSocket tcpSocker;    tcpSocker.connect(Address(host,"http"));    tcpSocker.write("GET " + path + " HTTP/1.1\r\n");    tcpSocker.write("Host: " + host + "\r\n");    tcpSocker.write("Connection: close \r\n");    tcpSocker.write("\r\n");    while(!tcpSocker.eof()){        auto buffer = tcpSocker.read();        cout<

第二个任务是实现一个字节流, 有读端和写端,写的数据可能没有完全读出去,所以是一个双端队列,结合提议,很容易想到以下实现

成员函数如下, 在现代C++中,可以直接声明初始化(编译很严格,必须初始化)

class ByteStream {  private:    // Your code here -- add private members as necessary.    // Hint: This doesn't need to be a sophisticated data structure at    // all, but if any of your tests are taking longer than a second,    // that's a sign that you probably want to keep exploring    // different approaches.    size_t capacity;    size_t read_count = 0;    size_t write_count = 0;    bool is_end = false;    bool _error{};  //!< Flag indicating that the stream suffered an error.    std::deque
buffer = {};}

 以下是函数实现

ByteStream::ByteStream(const size_t capacity_):capacity(capacity_){}size_t ByteStream::write(const string &data) {    auto data_size = data.size();    if(data_size>capacity-buffer.size()){        data_size = capacity-buffer.size();    }    write_count += data_size;    for(size_t i=0;i
buffer.size()){ peek_size = buffer.size(); } return string().assign(buffer.begin(),buffer.begin()+peek_size);}//! \param[in] len bytes will be removed from the output side of the buffervoid ByteStream::pop_output(const size_t len) { auto pop_size = len; if(pop_size>buffer.size()){ pop_size = buffer.size(); } read_count++; for(size_t i=0;i
buffer.size()){ read_size = buffer.size(); } read_count += read_size; string str = string().assign(buffer.begin(),buffer.begin()+read_size); for(size_t i=0;i

接下来要做的事,利用GDB调试程序,直到完成所以测试。

转载地址:http://ihqv.baihongyu.com/

你可能感兴趣的文章
Mysql学习总结(56)——MySQL用户管理和权限设置
查看>>
Mysql学习总结(57)——MySQL查询当天、本周、本月、上周、本周、上月、距离当前现在6个月数据
查看>>
Mysql学习总结(58)——深入理解Mysql的四种隔离级别
查看>>
Mysql学习总结(59)——数据库分库分表策略总结
查看>>
Mysql学习总结(5)——MySql常用函数大全讲解
查看>>
Mysql学习总结(60)——并发量大、数据量大的互联网业务数据库设计规范总结
查看>>
Mysql学习总结(61)——MySQL优化之DBA级优化整理汇总
查看>>
Mysql学习总结(62)——MySQL连接com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link问题
查看>>
Mysql学习总结(63)——Mysql数据库架构方案选择与分析
查看>>
Mysql学习总结(64)——Mysql配置文件my.cnf各项参数解读
查看>>
Mysql学习总结(65)——项目实战中常用SQL实践总结
查看>>
Mysql学习总结(66)——设置MYSQL数据库编码为UTF-8
查看>>
Mysql学习总结(67)——MYSQL慢查询日志
查看>>
Mysql学习总结(68)——MYSQL统计每天、每周、每月、每年数据 SQL 总结
查看>>
Mysql学习总结(69)——Mysql EXPLAIN 命令使用总结
查看>>
Mysql学习总结(6)——MySql之ALTER命令用法详细解读
查看>>
Mysql学习总结(70)——MySQL 优化实施方案
查看>>
Mysql学习总结(71)——MySQL 重复记录查询与删除总结
查看>>
Mysql学习总结(71)——数据库介绍(MySQL安装 体系结构、基本管理)再回顾
查看>>
Mysql学习总结(73)——MySQL 查询A表存在B表不存在的数据SQL总结
查看>>