# 说明文档结构:
1. 代码结构
2. 数据结构
3. 程序逻辑
4. 关于超时与拥塞控制
5. 关于并发运行
6. 关于开发过程中的问题
7. 程序运行结果图
# 一、代码结构
1. peer.c:程序的主结构,主函数入口,实现了对用户输入命令的处理和以及对接收到的不同类型数据包处理
2. queue.c/h:队列数据结构的封装文件并实现了队列的一系列操作的函数
3. task.c/h:上传和下载任务的函数分装文件,定义了任务、连接等结构,实现了对上传和下载的控制操作函数
4. timer.c/h:计算时间辅助文件,实现了计算两个时间的差、设定时间等函数
5. transfer.c/h:传送和文件处理的函数封装文件,实现了传送过程中对接收数据、文件的处理函数,以及发送数据包函数
6. udpPacket.c/h:数据包类型的定义封装文件,实现了不同类型数据包的生成函数以及网络、本地类型转换的辅助函数对于project给的文件,除了peer.c文件,其他没有进行修改,所以只列
出了新加入的一下文件及其用途。
文件定义函数与作用:
### peer.c:
```c++
//处理接收到的各个包的类型对应的函数声明,传入参数为接收到的数据包以及数据发送方
void handle_whohas(data_packet_t *pkt, bt_peer_t *peer);
void handle_ihave(data_packet_t *pkt, bt_peer_t *peer);
void handle_get(data_packet_t *pkt, bt_peer_t *peer);
void handle_data(data_packet_t *pkt, bt_peer_t *peer);
void handle_ack(data_packet_t *pkt, bt_peer_t *peer);
//根据地址获取对应的对等方
bt_peer_t *get_peer(struct sockaddr_in addr);
//用于记录窗口改变大小的函数,根据传入的参数记录
void log_window(int conn_id, int window, int time, int ssthresh, int isslows tart);
//处理time_out的线程函数,创建后sleep 1秒,期间线程可被停止,1秒开始处理超时
handle_time_out_thread(void *arg);
```
### queue.c/h:
```c++
//获取队列空间,并初始化队列
queue *make_queue();
//初始化队列
void init_queue(queue *);
//清空队列
void free_queue(queue *, int);
//将数据加入队列
void enqueue(queue *, void *);
//将数据提取出队列
void *dequeue(queue *);
```
### task.c/h:
```c++
//根据传入的chunk哈希和id用于初始化chunk
chunk_t *make_chunk(int id, const uint8_t *sha1);
//释放chunk
void free_chunk(chunk_t *chunk);
//初始化任务池
task_t *init_task(const char *, const char *, int);
//根据outputfile和getchunkfile初始化任务
int check_task(task_t *task);
//检测到当前任务未完成,继续下载任务
void continue_task(task_t *task, down_pool_t *pool, int sock);
//结束下载任务
task_t *finish_task(task_t *task);
//初始化上传池
void init_up_pool(up_pool_t *pool, int max_conn);
//根据对等方信息从上传池获取上传连接
up_conn_t *get_up_conn(up_pool_t *, bt_peer_t *);
//根据传入对等方地址和数据包数组的初始化上传连接并加入上传池
up_conn_t *add_to_up_pool(up_pool_t *pool, bt_peer_t *peer, data_packet_t ** pkts);
//根据对等方信息将上传连接从上传池移除
void remove_from_up_pool(up_pool_t *pool, bt_peer_t *peer);
//根据传入对等方地址和数据包数组的初始化上传连接
up_conn_t *make_up_conn(bt_peer_t *peer, data_packet_t **pkts);
//初始化下载池
init_down_pool(down_pool_t *pool, int max_conn);
//根据传入对等方找到对应的下载连接
down_conn_t *get_down_conn(down_pool_t *, bt_peer_t *);
//根据传入对等方地址和数据包数组的初始化下载连接并加入下载池
down_conn_t *add_to_down_pool(down_pool_t *pool, bt_peer_t *peer, chunk_t *c hunk);
//根据对等方信息移除相应的下载连接
void remove_from_down_pool(down_pool_t *pool, bt_peer_t *peer);
//根据传入对等方地址和chunk的初始化下载连接
down_conn_t *make_down_conn(bt_peer_t *peer, chunk_t *chunk);
//根据哈希值找到任务中对应的chunk,将传入的对等方加入其下载源,以更新可用对等方
void available_peer(task_t *task, uint8_t *sha1, bt_peer_t *peer);
//根据传入的ihave的哈希值,选择一个chunk进行下载
chunk_t *choose_chunk(task_t *task, queue *chunks, bt_peer_t *peer);
//清空失效的节点
int remove_stalled_chunks(down_pool_t *pool);
//清除未回应的对等方
void remove_unack_peers(up_pool_t *pool, int sock);
//寻找peer队列中是否存在这个peer
int check_peers(queue *peers, bt_peer_t *peer);
```
### timer.c/h:
```c++
//对传入的时间赋值当前时间
void timer_start(struct timeval *);
//计算两个时间的差值,返回为毫秒
int timer_diff(struct timeval *starter, struct timeval *now);
//计算传入时间与当前时间的差值
int timer_diff_now(struct timeval *starter);
```
### transfer.c/h:
```c++
//根据传入的get_chunk_file生成需要发送的whohas包队列
queue *init_whohas_packet_queue(const char *);
//根据传入的哈希值队列生成ihave包队列
queue *init_ihave_packet_queue(queue *);
//根据传入的哈希值寻找对应的chunk,并根据chunk生成data包
//由于每个chunk有512k,数据包只能有1500字节,所以每次读取1024字节生成一个data 包,总共生成512包
data_packet_t **init_data_packet_array(uint8_t *sha);
//读取chunkfile,将内容转化为二进制哈希值的队列
//此处chunkfile为getchunkfile和haschunkfile,
queue *chunk_file_to_queue(const char *chunk_file);
//将haschunkfile内容转为队列
init_has_chunks(const char *);
//根据传入的whohas队列,寻找本地拥有其中的某些项,并返回
//用于处理收到whohas包时找到本地拥有的chunk
queue *which_i_have(queue *);
//根据传入的数据生成哈希值队列
//用于处理whohas包和ihave包的数据
queue *data2chunks_queue(void *);
//根据传入的哈希值队列,生成对应的包队列
//生成的包类型为whohas和ihave类型,根据传入的参数决定哪种类型
queue *init_chunk_packet_queue(queue *chunks, data_packet_t *(*make_chunk_pa cket)(short, void *));
//向特定的对等方发送ihava包,将队列的包全部发送
void send_pkts(int, struct sockaddr *, queue *);
//向所有对等方发送队列中whohas包,用于处理get命令时使用发送whohas包
void send_whohas(int sock, queue *whohas_pkts);
//向特定对等方发送数据包
void send_data_packets(up_conn_t *, int, struct sockaddr *);
//发送包的接口,发送所以包统一调用该接口,该接口调用spiffy_send发送数据
void send_packet(int, data_packet_t *, struct sockaddr *);
```
### udpPacket.c/h:
```c++
//根据传入数据初始化数据包
void init_packet(data_packet_t *, char, short, u_int, u_int, char *);
//根据传入数据创建数据包,初始化数据包
data_packet_t *make_packet(char, short, u_int, u_int, char *);
void free_packet(data_packet_t *);
//创建不同类型的数据包
data_packet_t *make_whohas_packet(short, void *);
data_packet_t *make_ihave_packet(short, void *);
data_packet_t *make_get_packet(short, char *);
data_packet_t *make_data_packet(short, uint, uint, char *);
data_packet_t *make_ack_packet(uint, uint);
//将数据包头部转为网络格式
void host2net(data_packet_t *);
//将数据包头部转为本地格式
void net2host(data_packet_t *);
//判断数据包是否合法,不合法返回-1,合法返回数据包类型
int packet_is_legal(void *);
```
# 二、数据结构
### queue.h:
```c++
//队列中的节点
typedef struct node {
void *data;//节点数据
struct node *next;//下一个节点
} node;
//队列,用于存储过程中的各类链表数据
typedef struct queue {
node *head;//队列头部
node *tail;//队列�
没有合适的资源?快使用搜索试试~ 我知道了~
基于C语言实现计算机网络技术【100011834】
共40个文件
c:16个
h:12个
xml:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 47 浏览量
2023-04-11
16:18:26
上传
评论
收藏 950KB ZIP 举报
温馨提示
peer.c:程序的主结构,主函数入口,实现了对用户输入命令的处理和以及对接收到的不同类型数据包处理 queue.c/h:队列数据结构的封装文件并实现了队列的一系列操作的函数 task.c/h:上传和下载任务的函数分装文件,定义了任务、连接等结构,实现了对上传和下载的控制操作函数 timer.c/h:计算时间辅助文件,实现了计算两个时间的差、设定时间等函数 transfer.c/h:传送和文件处理的函数封装文件,实现了传送过程中对接收数据、文件的处理函数,以及发送数据包函数 udpPacket.c/h:数据包类型的定义封装文件,实现了不同类型数据包的生成函数以及网络、本地类型转换的辅助函数对于project给的文件,除了peer.c文件,其他没有进行修改,所以只列
资源推荐
资源详情
资源评论
收起资源包目录
100011834-基于C语言实现计算机网络技术.zip (40个子文件)
accomplishment_c
startcode
peer.c 18KB
queue.h 611B
sha.h 2KB
queue.c 1KB
debug.h 1KB
Makefile 1KB
bt_parse.h 957B
debugparse.pl 191B
spiffy.c 4KB
task.h 5KB
make_chunks.c 2KB
server.c 2KB
client.c 2KB
debug-text.h 261B
timer.c 505B
.idea
workspace.xml 16KB
misc.xml 240B
modules.xml 270B
encodings.xml 135B
test_input_buffer.c 432B
topo.map 201B
chunk.c 3KB
hupsim.pl 12KB
transfer.c 8KB
bt_parse.c 5KB
input_buffer.h 348B
nodes.map 182B
timer.h 366B
sha.c 15KB
spiffy.h 607B
transfer.h 2KB
task.c 12KB
input_buffer.c 2KB
chunk.h 813B
debug.c 1KB
udpPacket.c 3KB
udpPacket.h 2KB
design.pdf 957KB
LICENSE 1KB
README.md 19KB
共 40 条
- 1
资源评论
神仙别闹
- 粉丝: 2674
- 资源: 7640
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功