CS144 lab0 笔记
生活随笔
收集整理的這篇文章主要介紹了
CS144 lab0 笔记
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
CS144 lab0 筆記
下學(xué)期就學(xué)習(xí)計算機網(wǎng)絡(luò)了,假期正好找個lab預(yù)習(xí)一下
配置
直接用 WSL2 + Clion(安裝在WSL2上) 做的實驗,還是比用vscode方便一些的
實驗 writing webget(入門)
這個實驗就是讓你熟悉一下網(wǎng)絡(luò)編程,做之前最好讀一下官方推薦的文檔
void get_URL(const string &host, const string &path) {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");sock1.shutdown(SHUT_WR); //關(guān)閉寫,告訴服務(wù)器不用等著了while (!sock1.eof()) {cout << sock1.read();}sock1.close();// 別忘了關(guān)掉}實現(xiàn)一個可靠的比特流(簡單)
實現(xiàn)一個buffer可以一邊讀一邊寫
-
buffer的大小不變
-
滿了就不能再寫了
-
讀到eof就不能讀了
-
不需要考慮多線程
本來是采用std::deque實現(xiàn)的,后來發(fā)現(xiàn)鏈表方式效率更高一些
byte_stream.hh
class ByteStream {private:bool _error = false; //!< Flag indicating that the stream suffered an error.bool _input_ended = false;size_t _capacity;size_t _buffer_size = 0; // capacity中已經(jīng)有的字符大小size_t _bytes_written = 0; // 計數(shù)器,記錄一共寫入多少size_t _bytes_read = 0; // 計數(shù)器,記錄一共讀取多少std::list<char> _stream{}; // streampublic:byte_stream.cc
#include "byte_stream.hh"// Dummy implementation of a flow-controlled in-memory byte stream.// For Lab 0, please replace with a real implementation that passes the // automated checks run by `make check_lab0`.// You will need to add private members to the class declaration in `byte_stream.hh`template <typename... Targs> void DUMMY_CODE(Targs &&... /* unused */) {}using namespace std; ByteStream::ByteStream(const size_t capacity) : _capacity(capacity) {}size_t ByteStream::write(const string &data) {size_t write_count = 0;for (const char c : data) {// not very efficient to do conditional in loopif (_capacity - _buffer_size <= 0)break;else {_stream.push_back(c);++_buffer_size;++_bytes_written;++write_count;}}return write_count; }//! \param[in] len bytes will be copied from the output side of the buffer string ByteStream::peek_output(const size_t len) const {const size_t peek_length = len > _buffer_size ? _buffer_size : len;auto it = _stream.begin();advance(it, peek_length);return string(_stream.begin(), it); }//! \param[in] len bytes will be removed from the output side of the buffer void ByteStream::pop_output(const size_t len) {size_t pop_length = len > _buffer_size ? _buffer_size : len;_bytes_read += pop_length;_buffer_size -= pop_length;while (pop_length--)_stream.pop_front(); }//! 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) {const string result = peek_output(len);pop_output(len);return result; }void ByteStream::end_input() { _input_ended = true; }bool ByteStream::input_ended() const { return _input_ended; }size_t ByteStream::buffer_size() const { return _buffer_size; }bool ByteStream::buffer_empty() const { return _stream.size() == 0; }bool ByteStream::eof() const { return _input_ended && buffer_empty(); }size_t ByteStream::bytes_written() const { return _bytes_written; }size_t ByteStream::bytes_read() const { return _bytes_read; }size_t ByteStream::remaining_capacity() const { return _capacity - _buffer_size; }總結(jié)
以上是生活随笔為你收集整理的CS144 lab0 笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 翻译脚本的使用
- 下一篇: Android文字图像识别并翻译的简单实