资源简介
数据链路层滑动窗口协议的设计与实现
选择重传,计算机网络实验,C文件datalink。c
CRC校验,效率60%
代码片段和文件信息
#include
#include
#include
#include “protocol.h“
#include “datalink.h“
void printData(char *p int size);
typedef unsigned char seq_nr;
typedef unsigned char frame_kind;
typedef struct{unsigned char data[PKT_LEN];} packet;
typedef enum{network_layer_ready physical_layer_ready frame_arrive data_timout ack_timeout chksum_error} event_type;
typedef enum{F T} mybool;
typedef struct
{
frame_kind kind; /* frame_DATA */
seq_nr ack;
seq_nr seq;
packet info; /* packet */
unsigned char padding[4]; /* memory space used for crc code 32 bit */
} frame;
mybool no_nak = T; /* no nak has beem yet */
static void Inc(seq_nr *seq)
{
*seq = (*seq + 1) % (MAX_SEQ + 1);
}
static mybool between(seq_nr a seq_nr b seq_nr c)
{
/*return T if a <= b circulaly; F otherwise*/
return ((a <= b) && (b < c)) || ((c < a) && (a <= b)) || ((b < c) && (c < a));
}
static void send(frame_kind fk seq_nr frame_nr seq_nr frame_expected packet buf[])
{
/* construct frame and send */
frame s; /* tmp variable */
s.kind = fk; /* s.kind = data/ack/nak */
if (fk == frame_DATA)
memcpy((char*)&s.info (char*)&buf[frame_nr % NR_BUFS] PKT_LEN); /* store the packet */
s.seq = frame_nr; /* only meaning for data */
s.ack = (frame_expected + MAX_SEQ) % (MAX_SEQ + 1);
if (fk == frame_NAK)
no_nak = F;
/* compute the crc code the crc code is in the padding memory */
*(unsigned int*) ((unsigned char*)&s + PKT_LEN + 3) = crc32((unsigned char*)&s PKT_LEN + 3);
/* send the frame to the physical layer */
send_frame((unsigned char*)&s sizeof(s));
dbg_event(“组装之后的 !!\n“);
printData((char*)&s sizeof(s));
//printf(“CRC send“);
//printData(s.padding4);
/* warning: short may not be 2 bytes according to the computer */
if (fk == frame_DATA)
dbg_frame(“Send DATA seq:%d ack:%d ID %d\n“ s.seq s.ack *(short *)(&s.info));
else if (fk == frame_ACK)
dbg_frame(“Send ACK seq:%d\n“ s.seq);
else if (fk == frame_NAK)
dbg_frame(“Send NAK seq:%d\n“ s.seq);
if (fk == frame_DATA) /* start the timeout if timeout repeat transmit */
start_timer(frame_nr % NR_BUFS DATA_TIMER);
stop_ack_timer(); /* no need for separate ack frame */
}
void protocol6()
{
int midstop=0;
seq_nr ack_expected = 0; /* lower edge of sender‘s window */
seq_nr next_frame_to_send = 0; /* upper edge of sender‘s window */
seq_nr frame_expected = 0; /* lower edge of receiver‘s window */
seq_nr too_far = NR_BUFS; /* uppder edge receiver‘s window + 1*/
packet out_buf[NR_BUFS]; /* buffers for the outbound stream*/
packet in_buf[NR_BUFS]; /* buffers for the inbound stream*/
mybool arrived[NR_BUFS] = {F}; /* inbound bit map intial to all F */
seq_nr nbuffered = 0; /* the size of buffered */
frame r; /* tmp variable */
int arg = MAX_SEQ + 1; /* para */
int len = 0; /* the length of received packet*/
unsigned crc_ret = 0;
event_type even
评论
共有 条评论