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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈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…