考研 408 × 求职面试
知识点 · 计算机网络 · 基础
每日一题 · 计算机网络
解析
面试小贴士 · 计算机网络
代码实现 · C
#include <stdio.h>
typedef struct { char type; int seq; } Msg;
int main(void) {
Msg msgs[] = {{'D',0},{'D',2},{'D',1}};
int expected = 0;
for (int i = 0; i < 3; i++) {
// 语法:规定报文由 type 和 seq 组成,并检查取值范围
if (msgs[i].type != 'D' || msgs[i].seq < 0 || msgs[i].seq > 99) {
printf("语法错误\n");
continue;
}
// 语义:type 为 D 表示数据报文(本例已在语法中检查)
// 时序:序号必须等于当前期望值,保证按序接收
if (msgs[i].seq != expected) {
printf("时序错误:期望%d,收到%d\n", expected, msgs[i].seq);
continue;
}
printf("接收数据序号%d\n", msgs[i].seq);
expected++;
}
return 0;
}