cs144建議我們使用Modern C++來完成所有的lab,關于modern c++的全面的用法可以在(http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines)獲取。
創新互聯專注于企業全網營銷推廣、網站重做改版、東臺網站定制設計、自適應品牌網站建設、html5、商城建設、集團公司官網建設、外貿網站制作、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為東臺等各大城市提供網站開發制作服務。以下是一些代碼規范:
malloc()
和free()
new
和delete
(FILE)*x
,必要時使用C++的static_cast
char *s
,和C語言中的字符串函數strlen(), strcpy()
,應使用c++中std::string
const Address & address
)make format
來修改代碼風格在本次熱身實驗中,我們將會利用操作系統預先存在的接口來實現一個簡單的TCP socket,并利用這個tcp socket來實現對網頁的請求。
使用OS的流套接字寫一個網絡程序這個部分比較簡單,只需要按照handout所說,看完TCPSocket,FileDescriptor,Socket和Address這幾個類的介紹,就能很快的完成這個程序,其實只需要看TCPSocket
class就行。這些源代碼都在libsponge\util
中。
在linux系統中,一切皆文件。
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請求的格式,這也就是我們的應用層數據,在通過socket后會封裝上TCP頭部再丟進網絡層傳輸。
內存中的可靠數據流我們要完成的任務是實現一個類似于管道的數據結構ByteStream,這個channel有兩端,一端是input side,負責向channel中輸入字符串,另一端是output side,負責從channel中讀取字符串并且取出來,在抽象的數據結構中來看,std::deque
雙端隊列最合適不過。
這個管道是有容量限制的,其在初始化時就會規定好其capacity,作為在任意時刻channel內的大字符量。隨著output side對channel內字符串的讀取,input side被允許寫入更多的字符串,這意味著ByteStream可以傳輸比其自身capacity更大的數據,換句話就是ByteStream的容量理論上可以達到無限,只要input side持續地write。
在對ByteStream的功能熟悉后,我們就可以在libsponge\byte_stream.hh
和libsonge\byte_stream.cc
的骨架代碼中完成我們的實現。這里需要特別強調一下**EOF
的狀態:**end of file
是指在input side終止了輸入的前提下,output side從channel中讀取完了所有的字符串,此時ByteStream中的數據為空,并且不會再有數據輸入。
對于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類的方法的實現:
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
結果如下:
你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
本文題目:CS144-Lab0解析-創新互聯
網頁網址:http://vcdvsql.cn/article14/cccpge.html
成都網站建設公司_創新互聯,為您提供小程序開發、建站公司、品牌網站建設、手機網站建設、做網站、網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯