成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

CS144-Lab0解析-創(chuàng)新互聯(lián)

講在開頭

cs144建議我們使用Modern C++來完成所有的lab,關(guān)于modern c++的全面的用法可以在(http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines)獲取。

創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、東臺(tái)網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、html5、商城建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為東臺(tái)等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

以下是一些代碼規(guī)范:

  • 不要使用malloc()free()
  • 不要使用newdelete
  • 在不得不使用指針時(shí)應(yīng)使用智能指針(unique_ptr或者shared_ptr),而不是原始指針(*)
  • 避免使用模板、多線程、鎖和虛函數(shù)
  • 避免使用C風(fēng)格的類型轉(zhuǎn)換(FILE)*x,必要時(shí)使用C++的static_cast
  • 避免使用C風(fēng)格的字符串char *s,和C語言中的字符串函數(shù)strlen(), strcpy(),應(yīng)使用c++中std::string
  • 盡可能的使用常量引用(e.g.:const Address & address
  • 盡可能用const修飾變量和方法
  • 避免全局變量,盡可能縮小每個(gè)變量的作用域
  • 在提交之前,請使用make format來修改代碼風(fēng)格

在本次熱身實(shí)驗(yàn)中,我們將會(huì)利用操作系統(tǒng)預(yù)先存在的接口來實(shí)現(xiàn)一個(gè)簡單的TCP socket,并利用這個(gè)tcp socket來實(shí)現(xiàn)對網(wǎng)頁的請求。

使用OS的流套接字寫一個(gè)網(wǎng)絡(luò)程序

這個(gè)部分比較簡單,只需要按照handout所說,看完TCPSocket,F(xiàn)ileDescriptor,Socket和Address這幾個(gè)類的介紹,就能很快的完成這個(gè)程序,其實(shí)只需要看TCPSocketclass就行。這些源代碼都在libsponge\util中。

Inheritance graph

在linux系統(tǒng)中,一切皆文件。

webget.c

void get_URL(const string &host, const string &path) {TCPSocket tsk;
    tsk.connect(Address(host,"http"));

    tsk.write("GET " + path + " HTTP/1.1\r\n" +
                "Host: " + host + "\r\n" +
                "Connection: close\r\n\r\n");
    string str;
    while(!tsk.eof()) {tsk.read(str);
        cout<< str;
    }

    tsk.close();
}

注意的是write的字符串要寫成HTTP請求的格式,這也就是我們的應(yīng)用層數(shù)據(jù),在通過socket后會(huì)封裝上TCP頭部再丟進(jìn)網(wǎng)絡(luò)層傳輸。

內(nèi)存中的可靠數(shù)據(jù)流

我們要完成的任務(wù)是實(shí)現(xiàn)一個(gè)類似于管道的數(shù)據(jù)結(jié)構(gòu)ByteStream,這個(gè)channel有兩端,一端是input side,負(fù)責(zé)向channel中輸入字符串,另一端是output side,負(fù)責(zé)從channel中讀取字符串并且取出來,在抽象的數(shù)據(jù)結(jié)構(gòu)中來看,std::deque雙端隊(duì)列最合適不過。

這個(gè)管道是有容量限制的,其在初始化時(shí)就會(huì)規(guī)定好其capacity,作為在任意時(shí)刻channel內(nèi)的大字符量。隨著output side對channel內(nèi)字符串的讀取,input side被允許寫入更多的字符串,這意味著ByteStream可以傳輸比其自身capacity更大的數(shù)據(jù),換句話就是ByteStream的容量理論上可以達(dá)到無限,只要input side持續(xù)地write。

在對ByteStream的功能熟悉后,我們就可以在libsponge\byte_stream.hhlibsonge\byte_stream.cc的骨架代碼中完成我們的實(shí)現(xiàn)。這里需要特別強(qiáng)調(diào)一下**EOF的狀態(tài):**end of file是指在input side終止了輸入的前提下,output side從channel中讀取完了所有的字符串,此時(shí)ByteStream中的數(shù)據(jù)為空,并且不會(huì)再有數(shù)據(jù)輸入。

對于ByteStream類,我添加的私有成員如下:

class ByteStream {private:
    // Your code here -- add private members as necessary.
    size_t _capacity;
    bool _eof = false;
    std::deque_buffer;
    size_t _total_write;
    size_t _total_read;
    

    bool _error{};  //!< Flag indicating that the stream suffered an error.

  ...
}

ByteStream類的方法的實(shí)現(xiàn):

ByteStream::ByteStream(const size_t capa) : _capacity(capa), _eof(false), _buffer(), _total_write(0), _total_read(0) {}

size_t ByteStream::write(const string &data) {size_t remain_size = _capacity - _buffer.size();
    size_t len = min(remain_size, data.length());
    for (size_t i = 0; i< len; i++)
        _buffer.push_back(data[i]);
    _total_write += len;
    return len;
}

//! \param[in] len bytes will be copied from the output side of the buffer
string ByteStream::peek_output(const size_t len) const {size_t l = min(len, _buffer.size());
    string output = "";
    for (size_t i = 0; i< l; i++)
        output += _buffer[i];

    return output;
}

//! \param[in] len bytes will be removed from the output side of the buffer
void ByteStream::pop_output(const size_t len) {size_t l = min(len, _buffer.size());
    _total_read += l;
    while (l--)
        _buffer.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) {string str = peek_output(len);
    pop_output(len);
    return str;
}

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

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

size_t ByteStream::buffer_size() const {return _buffer.size(); }

bool ByteStream::buffer_empty() const {return _buffer.empty(); }

bool ByteStream::eof() const {return _eof && _buffer.empty(); }

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

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

size_t ByteStream::remaining_capacity() const {return _capacity - _buffer.size(); }

通過下列指令完成編譯;

mkdir build
cd build
cmake ..
make
make check_lab0

結(jié)果如下:

image-20221219011624614

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

本文題目:CS144-Lab0解析-創(chuàng)新互聯(lián)
網(wǎng)頁網(wǎng)址:http://jinyejixie.com/article14/cccpge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、建站公司、品牌網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司
黄平县| 平舆县| 汶川县| 称多县| 吉安市| 宁津县| 通化县| 天镇县| 赞皇县| 西乌| 石屏县| 增城市| 渑池县| 龙口市| 凌云县| 萝北县| 吴堡县| 宿松县| 于田县| 芮城县| 修水县| 昌江| 井冈山市| 斗六市| 宁晋县| 和平县| 深泽县| 和平区| 大庆市| 邢台市| 吕梁市| 米泉市| 南开区| 拜城县| 兴海县| 红原县| 邛崃市| 景德镇市| 贞丰县| 枣庄市| 新乐市|