IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> CS144 Lab0 -> 正文阅读

[网络协议]CS144 Lab0

写在前面:

这周开始学习斯坦福CS144计算机网络,之前看过哈工大的计算机网络的mooc以及计算机网络自顶向下这本书,对五层网络模型以及TCP/IP协议有过一定了解,但是没有实际的用代码实践过,因此选择CS144这门课作为实践。

我使用的电脑是M1 MAC,用的是CS144建议的MTU虚拟机以及提供的对应的虚拟机镜像。这里只记录代码实验。

实验部分:

一、writing a network program using an OS stream socket

  • 这个实验是用TCP socket写一个webget程序,实现telnet获取网页的功能,并将回复信息打印出来
  • 向web server请求网页的格式是:
    • GET /PATH HTTP/1.1
    • HOST: URL
    • Connection:close
    • 在lab中用/r/n表示换行,当web server收到close消息时,会向client发送EOF表明服务器回复完毕。
  • 代码如下:
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";

    string request="GET "+path+             
        "HTTP/1.1\r\nHost:"+host+"\r\nConnection:close\r\n\r\n";
    TCPSocket localhost;
    localhost.connect(Address(host,"http"));
    localhost.write(request);
    while(!localhost.eof()){
	    cout<<localhost.read(1);
    }
    localhost.close();
}
  • 实验结果如下:

二、An in-memory reliable byte stream

  • 本实验是实现一个字节流类,字节流类在内存中的缓存大小有限
  • writer允许向字节流中写入有限个字节,超过限制则不再写入,直到reader将字节流中的字节读完
  • 字节流类中设置了如下类成员对象:
    • cap:表明字节流允许存储的字节大小
    • sz:表明字节流中此时已经存储了的字节大小
    • totalwrite:writer向字节流中总共写入的字节数
    • totalread:reader向字节流中总共读取的字节数
    • end:表明input是否结束
    • memory:存储在字节流中的字节
  • 代码如下:
//byte_stream.hh
class ByteStream {
  private:
    // Your code here -- add private members as necessary.

    size_t cap;
    size_t sz;
    size_t totalwrite,totalread;
    std::string memory;
    bool end;


    // 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.
......

//byte_stream.cc
ByteStream::ByteStream(const size_t capacity):cap(capacity),sz(0),totalwrite(0),totalread(0),memory(""),end(false){}

size_t ByteStream::write(const string &data) {
    if(sz==cap){
	    return 0;
    }
    else if(data.size()+sz<=cap){
	    memory+=data;
	    sz+=data.size();
	    totalwrite+=data.size();
	    return data.size();
    }
    memory+=data.substr(0,cap-sz);
    size_t res=cap-sz;
    sz=cap;
    totalwrite+=res;
    return res;
}

//! \param[in] len bytes will be copied from the output side of the buffer
string ByteStream::peek_output(const size_t len) const {
    if(len>=sz) return memory;
    return memory.substr(0,len);
}

//! \param[in] len bytes will be removed from the output side of the buffer
void ByteStream::pop_output(const size_t len) { 
    if(len>=sz){
	    memory="";
	    totalread+=sz;
	    sz=0;
    }	    
    else{
	    totalread+=len;
	    sz=sz-len;
	    memory=memory.substr(len);
    }
}

//! Read (i.e., copy and then pop) the next "len" bytes of the stream
//! \param[in] len bytes will be popped and returned
//! \returns a string
std::string ByteStream::read(const size_t len) {
    string res=peek_output(len);
    pop_output(len);
    return res;
}

void ByteStream::end_input() {end=true;}

bool ByteStream::input_ended() const {return end;}

size_t ByteStream::buffer_size() const { return sz; }

bool ByteStream::buffer_empty() const { return sz==0; }

bool ByteStream::eof() const { if(end && sz==0) return true;
	return false; }

size_t ByteStream::bytes_written() const { return totalwrite; }

size_t ByteStream::bytes_read() const { return totalread; }

size_t ByteStream::remaining_capacity() const { return cap-sz; }
  • 实验结果

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-10-22 21:53:48  更:2022-10-22 21:56:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 16:19:36-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码