CS144 lab0: warmup

Lab 0: networking warmup

1. 環境

依賴配置

 sudo apt update && sudo apt install git cmake gdb build-essential clang \clang-tidy clang-format gcc-doc pkg-config glibc-doc tcpdump tshark

g+±13配置

  • ppa中科大源
# deb https://ppa.launchpadcontent.net/ubuntu-toolchain-r/test/ubuntu/ jammy main
deb https://launchpad.proxy.ustclug.org/ubuntu-toolchain-r/test/ubuntu/ jammy main
# deb-src https://ppa.launchpadcontent.net/ubuntu-toolchain-r/test/ubuntu/ jammy main
  • 安裝g++-13 gcc-13
sudo apt install gcc-13
sudo apt install g++-13
  • 設定優先級
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 11
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 13
  • 版本檢查
g++ -v
gcc -v
  • cmake版本安裝
git clone https://gitee.com/mirrors/CMakesource.git
./configure
make && make install

更新apt源

sudo update-alternatives --install /usr/bin/cmake cmake /usr/local/bin/cmake 1 --force

2. 使用命令獲取網頁

2.1 http獲取網頁

telnet到服務器上

telnet cs144.keithw.org http

輸入http方法,指定主機,輸入完成后還需要按一次enter

GET /hello HTTP/1.1 \n
Host: cs144.keithw.ort \n
\n

輸入成功后可以得到

Hello, CS144!

的字樣。

2.2 smtp輸入郵箱

沒有內網權限,跳過這個實驗

2.3 本地服務器

在本地使用 netcat命令

  • 服務端
netcat -v -l -p 9090
  • 客戶端
telnet localhost 9090

之后分別在客戶端和服務端輸入,兩方都會分別顯示。
相當于一個echo-server

3. 使用系統socket編寫網絡程序

如果你了解socket編寫程序的步驟,加上你看懂了socket.ccaddress.cc這兩個類的代碼;
直接調用它的接口就好了。

  TCPSocket cln_skt;Address peer_address( host, "http");cln_skt.connect( peer_address );// Address pa = cln_skt.peer_address();// std::cout << pa.to_string() << std::endl;std::string http_req_str("GET ");http_req_str.append( path );http_req_str.append(" HTTP/1.1\r\nHost: cs144.keithw.org\r\nConnection: close\r\n\r\n");cln_skt.write( http_req_str);std::string rsp;while ( !cln_skt.eof()) {rsp.clear();cln_skt.read( rsp );std::cout << rsp;}cln_skt.close();
4. 實現內存中的可靠字節流
4.1 實驗描述

這個實驗的目的是,在內存中實現和在上面網絡通信中相類似的可靠字節流。
字節流是有限的,寫入端可以終止輸入;當讀入端讀到EOF時,就不能再繼續讀了。
這個字節流在內存中是需要控制大小的,字節流會控制在任意時刻寫入流的大小,它不會超過它的存儲容量,直到當讀入端新讀出了一些數據,寫入端才又可以重新寫東西。這個字節流是在單線程下工作的,不用考慮讀寫競爭的問題。

4.2 實現
  • bytestream.cc
#include "byte_stream.hh"using namespace std;ByteStream::ByteStream( uint64_t capacity ) : capacity_( capacity ) {}void Writer::push( string data ) noexcept
{// (void)data; // Your code here.if ( is_closed_ ) {this->error_ = true;return ;}auto sz = data.size();if ( sz > available_capacity()) {//std::cout << "string greater than capacity!!!" << std::endl;}auto pushBytes = std::min( sz, available_capacity());if ( 0 == pushBytes )return;// auto edit = std::find(data.begin(), data.begin() + pushBytes, EOF);// if ( edit != data.begin() + pushBytes) {//    pushBytes = static_cast<uint64_t>( std::distance(data.begin(), edit ) );//    is_closed_ = true;// }for (uint64_t i = 0; i < pushBytes; ++i) {if ( EOF == data[i] ) {pushBytes = i;is_closed_ = true;break;}// pushStr.push_back( data[i] );}tot_bytes_pushed_ += pushBytes;cur_bytes_buffered += pushBytes;// buffers.push( data.substr(0, pushBytes));// buffers.emplace( data.substr(0, pushBytes));buffers.emplace( std::string_view(data.begin(), data.begin() + pushBytes));}void Writer::close() noexcept
{is_closed_ = true;// Your code here.
}bool Writer::is_closed() const noexcept
{return is_closed_; // Your code here.
}uint64_t Writer::available_capacity() const noexcept
{// Your code here.return capacity_ - cur_bytes_buffered;
}uint64_t Writer::bytes_pushed() const noexcept
{return tot_bytes_pushed_; // Your code here.
}string_view Reader::peek() const noexcept
{if ( buffers.empty()) {return "";}return std::string_view{buffers.front().begin() + lazy_pointer, buffers.front().end()};// Your code here.
}void Reader::pop( uint64_t len ) noexcept
{if ( 0 == len ) {return;}if ( buffers.empty()) {return;}while ( len > 0 && cur_bytes_buffered > 0) {std::string& s = buffers.front();auto real_sz = s.size() - lazy_pointer;auto pop_bytes = min( static_cast<uint64_t>( real_sz), len);if ( len >= real_sz ) {buffers.pop();lazy_pointer = 0;}else {lazy_pointer += pop_bytes;// s.erase(0, len);}len -= pop_bytes;cur_bytes_buffered -= pop_bytes;tot_bytes_poped_  += pop_bytes;}//  (void)len; // Your code here.
}bool Reader::is_finished() const noexcept
{return is_closed_ && buffers.empty(); // Your code here.
}uint64_t Reader::bytes_buffered() const noexcept
{return  cur_bytes_buffered; // Your code here.
}uint64_t Reader::bytes_popped() const noexcept
{return tot_bytes_poped_; // Your code here.
}
  • bytestream.h
#pragma once#include <cstdint>
#include <string>
#include <string_view>
#include <queue>
#include <algorithm>
#include <iostream>class Reader;
class Writer;class ByteStream
{
public:explicit ByteStream( uint64_t capacity );// Helper functions (provided) to access the ByteStream's Reader and Writer interfacesReader& reader();const Reader& reader() const;Writer& writer();const Writer& writer() const;void set_error() noexcept{ error_ = true; };       // Signal that the stream suffered an error.bool has_error() const noexcept{ return error_; }; // Has the stream had an error?protected:// Please add any additional state to the ByteStream here, and not to the Writer and Reader interfaces.uint64_t capacity_;bool error_ {};std::queue<std::string> buffers{};bool is_closed_{ false };uint64_t cur_bytes_buffered{};uint64_t tot_bytes_pushed_{};uint64_t tot_bytes_poped_{};size_t lazy_pointer{};
};class Writer : public ByteStream
{
public:void push( std::string data ) noexcept; // Push data to stream, but only as much as available capacity allows.void close() noexcept;                  // Signal that the stream has reached its ending. Nothing more will be written.bool is_closed() const noexcept;              // Has the stream been closed?uint64_t available_capacity() const noexcept; // How many bytes can be pushed to the stream right now?uint64_t bytes_pushed() const noexcept;       // Total number of bytes cumulatively pushed to the stream
};class Reader : public ByteStream
{
public:std::string_view peek() const noexcept; // Peek at the next bytes in the buffervoid pop( uint64_t len ) noexcept;      // Remove `len` bytes from the bufferbool is_finished() const noexcept;        // Is the stream finished (closed and fully popped)?uint64_t bytes_buffered() const noexcept; // Number of bytes currently buffered (pushed and not popped)uint64_t bytes_popped() const noexcept;   // Total number of bytes cumulatively popped from stream
};/** read: A (provided) helper function thats peeks and pops up to `max_len` bytes* from a ByteStream Reader into a string;*/
void read( Reader& reader, uint64_t max_len, std::string& out );

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/909410.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/909410.shtml
英文地址,請注明出處:http://en.pswp.cn/news/909410.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

StarRocks

StarRocks 是一個高性能的 分布式 MPP(Massively Parallel Processing)數據庫,主要用于 實時數據分析(Real-Time Analytics),是新一代的 OLAP 數據庫,對標 ClickHouse、Apache Doris 等。 ?? 一、StarRocks 是什么? StarRocks 是一個面向實時分析場景、支持高并發、高…

8088單板機8259中斷的軟件觸發測試

1.工作原理 8086和8088的中斷設計的是很巧妙的&#xff0c;比如給8259的IR1配置了一個中斷&#xff0c;中斷號為21H&#xff0c;那么當真個引腳出現高電平的時候&#xff0c;就會觸發相應上的中斷響應。但&#xff0c;這不是唯一能夠觸發21H中斷的方法&#xff0c;還可以通過軟…

TC3xx中PFLASH緩存對XCP標定常量的影響

1、TC3xx中PFLASH緩存&#xff08;Cache&#xff09;對XCP標定的影響 XCP的映射用到TC3XX的Overlay功能需要使用一段Pflash內存。 Pflash數據有兩個段區。分別為0x80000000和0xA0000000為起始地址的PFLASH段。 如上&#xff0c;兩段數據的區別是一個段8有CACHE緩存&#xff0c;…

代碼審計服務:如何解決誤報與漏報難題,保障軟件安全?

代碼審計服務在保障軟件質量、安全合規等方面扮演著關鍵角色&#xff0c;特別是在數字化浪潮席卷而來的今天&#xff0c;其重要性日益顯著。它能揭露代碼中的不足&#xff0c;進而為軟件開發提供有力的效率和安全性保障。 誤報與漏報難題 常規的代碼審查工具&#xff0c;其錯…

web方向第一次考核內容

一.考核內容 Web組大一下考核之HTML、CSS 1.為什么要清除浮動&#xff08;4)&#xff0c;清除浮動的方法有哪些&#xff1f;(6)&#xff08;至少兩種&#xff09; 2.怎么實現左邊左邊寬度固定右邊寬度自適應的布局&#xff1f;(10) 3.講講flex:1;(10) 4.怎么實現移動端適配不同…

HarmonyOS 5 Cordova有哪些熱門插件?

以下是 HarmonyOS 5 環境下 Cordova 的熱門插件及核心代碼實現&#xff08;綜合實際開發場景高頻使用&#xff09;&#xff1a; 一、核心工具類插件 1. ?高性能圖片壓縮插件? ?功能?&#xff1a;直接調用鴻蒙 ImageSource API 實現硬件級加速壓縮 ?代碼實現?&#xff…

Cesium圓錐漸變色實現:融合頂點著色器、Canvas動態貼圖與靜態紋理的多方案整合

在Cesium中渲染圓錐體時&#xff0c;無論采用頂點著色器、Canvas動態貼圖還是靜態圖片貼圖&#xff0c;其漸變色均需滿足以下條件&#xff1a; 圓形結構&#xff1a;漸變范圍限定在圓錐底面的圓形區域內。徑向擴散&#xff1a;顏色從圓心向外逐步變化&#xff08;如紅→黃→藍…

周末復習1

質量管理包括質量規劃&#xff0c;質量保證&#xff0c;質量控制。質量管理體系要定期執行內部審核和管理評審。二者都屬于質量保證過程。 實施質量保證的方法很多&#xff0c;過程分析屬于實施質量保證的常用方法。 采購管理過程包括編制采購計劃,實施采購,控制采購和結束采購…

英飛凌亮相SEMICON China 2025:以SiC、GaN技術引領低碳化與數字化未來

在剛剛落幕的SEMICON China 2025上&#xff0c;全球半導體行業再度匯聚上海&#xff0c;共同探討產業未來。本屆展會以“跨界全球?心芯相聯”為主題&#xff0c;覆蓋芯片設計、制造、封測、設備及材料等全產業鏈&#xff0c;充分展現了半導體技術的最新突破與創新趨勢。 作為…

工業路由器賦能倉庫消防預警,智慧消防物聯網解決方案

在現代物流與倉儲行業蓬勃發展的當下&#xff0c;倉庫的規模與存儲密度不斷攀升&#xff0c;消防預警的重要性愈發凸顯。傳統消防系統在應對復雜倉庫環境時&#xff0c;預警滯后、設備聯動不暢、數據管理困難等弊端逐漸暴露。為了有效解決這些問題&#xff0c;工業路由器作為物…

【開發常用命令】:服務器與本地之間的數據傳輸

服務器與本地之間的數據傳輸 本地給服務器上傳數據 scp /path/to/local_file usernameremotehost:/path/to/remote_directory例如 scp test.txt root192.168.1.xxx:/test # test.txt 需要上傳到服務器的文件&#xff0c;如果非當前路徑&#xff0c;使用文件的相對路徑或絕對…

springboot + nacos + k8s 優雅停機

1 概念 優雅停機是什么&#xff1f;網上說的優雅下線、無損下線&#xff0c;都是一個意思。 優雅停機&#xff0c;通常是指在設備、系統或應用程序中止運作前&#xff0c;先執行一定的流程或動作&#xff0c;以確保數據的安全、預防錯誤并保證系統的整體穩定。 一般來說&…

Python 標準庫之 math 模塊

1. 前言 math 模塊中包含了各種浮點運算函數&#xff0c;包括&#xff1a; 函數功能floor向下取整ceil向上取整pow指數運算fabs絕對值sqrt開平方modf拆分小數和整數fsum計算列表中所有元素的累加和copysign復制符號pi圓周率e自然對數 2. math.floor(n) 函數 math.floor(n) 的…

6.14星期六休息一天

Hey guys, Today’s Saturday, and I didn’t have to go to work, so I let myself sleep in a bit — didn’t get up until 8 a.m. My cousin invited me over to his place. He lives in a nearby city, about 80 kilometers away. But honestly, after a long week, I …

event.target 詳解:理解事件目標對象

event.target 詳解&#xff1a;理解事件目標對象 在 JavaScript 事件處理中&#xff0c;event.target 是一個關鍵屬性&#xff0c;它表示最初觸發事件的 DOM 元素。下面我將通過一個可視化示例詳細解釋其工作原理和使用場景。 <!DOCTYPE html> <html lang"zh-C…

Flutter 小技巧之:實現 iOS 26 的 “液態玻璃”

隨著 iOS 26 發布&#xff0c;「液態玻璃」無疑是熱度最高的標簽&#xff0c;不僅僅是因為設計風格大變&#xff0c;更是因為 iOS 26 beta1 的各種 bug 帶來的毛坯感讓 iOS 26 沖上熱搜&#xff0c;比如通知中心和控制中心看起來就像是一個半成品&#xff1a; 當然&#xff0c;…

Android工程中FTP加密傳輸與非加密傳輸的深度解析

詳細的FTP傳輸實現方案&#xff0c;包括完整代碼、安全實踐、性能優化和實際應用場景分析。 一、FTP傳輸類型對比表&#xff08;增強版&#xff09; 特性非加密FTPFTPS (FTP over SSL/TLS)SFTP (SSH File Transfer Protocol)協議基礎FTP (RFC 959)FTP SSL/TLS (RFC 4217)SSH…

C# 枚 舉(枚舉)

枚舉 枚舉是由程序員定義的類型&#xff0c;與類或結構一樣。 與結構一樣&#xff0c;枚舉是值類型&#xff1a;因此直接存儲它們的數據&#xff0c;而不是分開存儲成引用和數據。枚舉只有一種類型的成員&#xff1a;命名的整數值常量。 下面的代碼展示了一個示例&#xff0c…

一文詳解前綴和:從一維到二維的高效算法應用

文章目錄 一、一維前綴和?1. 基本概念?2. C 代碼實現?3. 應用場景? 二、二維前綴和1. 基本概念?2. C 代碼實現?3. 應用場景? 三、總結? 在算法競賽和日常的數據處理工作中&#xff0c;前綴和是一種極其重要的預處理技術。它能夠在常數時間內回答多次區間查詢&#xff0…

windows 開發

文章目錄 環境搭建數據庫關鍵修改說明&#xff1a;在代碼中使用該連接字符串&#xff1a;注意事項&#xff1a;實際使用 都說幾天創造一個奇跡&#xff0c;現在是真的這樣了&#xff0c;Just do it! 環境搭建 數據庫 需要下載這個SQL Server數據庫&#xff0c;然后每次Visua…