strcpy(a,"0");
p=i;
q=j;
while(s1[p]==s2[q]&&s2[p]!
=0){
a[m]=s2[q];
p++;
q++;
m++;
}
if(strlen(a)>strlen(b))
{
strcpy(b,a);
}
m=0;
}
}
⑤
四、调试记录
给出测试用例
编号
用例描述
测试结果
修改情况
分析
①
21345678910
12345678910
无
程序运行正确
②
在f1中输入asd
生成了f2,打开后里面有asd
无
程序运行正确
③
在屏幕中输入12345680
12345770
12345860
屏幕上输出:
比平均成绩高的学生:
12345680
无
程序运行正确
④
输入f1=abcdef
输入f2=abcgkt
最长公子串为:
abc
无
程序运行正确
⑤
五、创新说明
①用户可以自行定义数据,根据用户输入的数组进行排序
②将f1的内容同时在屏幕上输出,用户可以直接查看文件内容
③用户可以自行定义学生个数以及在屏幕上输入学生数据,并且将成绩单独放到另一数组排序后根据序号对结构体排序
④用户可以输入任一字符串进行对比
⑤
一、程序实践概述
1、题目名称:
Socket编程基础
(1)时间服务器
(2)远程文件备份服务器
2、时间进度:
8学时
3、开发环境:
Ubantu
二、问题分析
1、功能说明:
①编程实现时间服务器
编写一个网络时间服务器timeserver,该服务器能应具有如下功能:
够为网络上的用户提供时间服务,即为网络用户返回服务器的当前时间;
记录发出请求的网络用户的IP地址(保存到文件中),格式如下:
IP地址请求时间
编写时间服务客户端timeclient,该客户端能够向服务器发送时间服务请求,并把获得的时间返回给用户。
②编程实现远程文件备份服务器
分别采用TCP或UDP协议编写一个远程数据备份服务器,运行客户端将本地文件备份到远程的服务器中。
服务器的功能:
接受客户端请求,把客户端的文件进行备份(可以备份到指定的文件夹)。
客户端的功能:
与远程服务器进行连接,在连接后把本地的文件发送给远程备份服务器。
③设计并实现带身份认证的远程数据备份服务器(选作)
在实验②的基础上增加身份管理和认证功能:
2、解决方案:
服务器端过程就是socket->bind->listen->accept->Read,write
对于客户端则是socket->connect->read,write
三、方案设计
1、模块结构:
2、数据结构:
①文件操作,time_tt,socket
②文件操作,socket
3、总体流程:
4、关键算法:
①time_tt;
if(argc!
=2)//获得ip地址
{
printf("usage%s\n",argv[0]);
return0;
}
if((he=gethostbyname(argv[1]))==NULL)
{
printf("gethostbynameerror\n");
return0;
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("socket()error\n");
return0;
}
bzero(&server,sizeof(server));
=AF_INET;
=htons(PORT);
=*((structin_addr*)he->h_addr);
if(connect(sockfd,(structsockaddr*)&server,sizeof(server))==-1)
{
printf("connetc()error\n");
return0;
}
recv(sockfd,(void*)&t,sizeof(time_t),0);
printf("Timeis%s\n",ctime(&t));
②
5、界面设计:
直接使用终端界面进行
四、调试记录
给出测试用例
编号
用例描述
测试结果
修改情况
分析
1
在
connetc()错误
将ip改为
这里的ip地址应使用服务端的ip地址
2
在客户端输入client
isSatJul710:
48:
302018
此时结果正确
3
在客户端输入client客户端输入/home/login/Desktop/
服务端输入/home/login/Desktop/
在服务端的文件夹里找到,并且内容与f1一致
无
程序运行正确
五、创新说明
①拥有错误提示,即用户可以知道是哪个模块出错,方便进行检查
②拥有用户认证功能,即登录时需要特定的用户名跟密码,客户端在屏幕上输入文件路径上载,服务端再输入存贮的文件路径。
一、程序实践概述
1、题目名称:
Libpcap开发包使用
2、时间进度:
16学时
3、开发环境:
Ubantu
二、问题分析
1、功能说明:
(1)获取网络接口名字和掩码等信息
(2)捕获数据包(单个数据包和多个数据包两种情况)
(3)以太网数据报捕获
(4)ARP数据包捕获
2、解决方案:
使用QT图形化编程,将4个功能分别对应4个按钮点击事件,并把抓包结果输出在文本框中
三、方案设计
1、模块结构:
2、数据结构:
Libpcap
3、总体流程:
4、关键算法:
给出关键算法描述
voidgetip(){
charerror_content[PCAP_ERRBUF_SIZE];
structin_addrnet_ip_address;//网络地址
structin_addrnet_mask_address;//掩码地址
char*net_interface;//接口名字
char*net_ip_string;//网络地址字符串形式
char*net_mask_string;//掩码地址字符串形式
u_int32_tnet_ip;//网络地址
u_int32_tnet_mask;//掩码地址
net_interface=pcap_lookupdev(error_content);//获取网络接口
pcap_lookupnet(//获取网络和掩码地址
net_interface,
&net_ip,
&net_mask,
error_content
);
printf("接口名字为:
%s\n",net_interface);
=net_ip;
net_ip_string=inet_ntoa(net_ip_address);//网络地址转为字符串形式
printf("网络地址为:
%s\n",net_ip_string);
=net_mask;
net_mask_string=inet_ntoa(net_mask_address);//掩码地址转为字符串形式
printf("掩码地址为:
%s\n",net_mask_string);
}
voidgetpacket(){
charerror_content[PCAP_ERRBUF_SIZE];
structpcap_pkthdrprotocol_header;//数据包头
pcap_t*pcap_handle;//libpcap句柄
structbpf_programbpf_filter;//BPF过滤规则
charbpf_filter_string[]="ip";//过滤规则
constu_char*packet_content;//数据包内容
bpf_u_int32net_mask;//掩码地址
bpf_u_int32net_ip;//网络地址
char*net_interface;//网络接口
net_interface=pcap_lookupdev(error_content);//获取网络接口
pcap_lookupnet(//获取网络和掩码地址
net_interface,
&net_ip,
&net_mask,
error_content
);
pcap_handle=pcap_open_live(net_interface,BUFSIZ,1,0,error_content);
//网络接口,数据包大小,混杂模式,等待时间,错误信息
pcap_compile(pcap_handle,&bpf_filter,bpf_filter_string,0,net_ip);
//编译过滤原则
pcap_setfilter(pcap_handle,&bpf_filter);//设置过滤原则
packet_content=pcap_next(pcap_handle,&protocol_header);//捕获一个数据包
printf("从%s捕获了一个数据包\n",net_interface);
printf("数据包长度为:
%d\n",;
pcap_close(pcap_handle);
}
voidgetpackets(){
voidpacket_callback(u_char*argument,conststructpcap_pkthdr*packet_header,constu_char*packet_content){
staticintpacket_number=1;
printf("捕获数据包的个数为:
%d\n",packet_number);
packet_number++;
}
charerror_content[PCAP_ERRBUF_SIZE];
structpcap_pkthdrprotocol_header;//数据包头
pcap_t*pcap_handle;//libpcap句柄
structbpf_programbpf_filter;//BPF过滤规则
charbpf_filter_string[]="ip";//过滤规则
constu_char*packet_content;//数据包内容
bpf_u_int32net_mask;//掩码地址
bpf_u_int32net_ip;//网络地址
char*net_interface;//网络接口
net_interface=pcap_lookupdev(error_content);//获取网络接口
pcap_lookupnet(//获取网络和掩码地址
net_interface,
&net_ip,
&net_mask,
error_content
);
pcap_handle=pcap_open_live(net_interface,BUFSIZ,1,0,error_content);
//网络接口,数据包大小,混杂模式,等待时间,错误信息
pcap_compile(pcap_handle,&bpf_filter,bpf_filter_string,0,net_ip);
//编译过滤原则
pcap_setfilter(pcap_handle,&bpf_filter);//设置过滤原则
pcap_loop(pcap_handle,10,packet_callback,NULL);
pcap_close(pcap_handle);
}
voidgetethernet(){
structether_header{
u_int8_tether_dhost[8];//目的以太网地址
u_int8_tether_shost[8];//源以太网地址
u_int16_tether_type;//以太网类型
};
charerror_content[PCAP_ERRBUF_SIZE];
structpcap_pkthdrprotocol_header;//数据包头
pcap_t*pcap_handle;//libpcap句柄
structbpf_programbpf_filter;//BPF过滤规则
charbpf_filter_string[]="ip";//过滤规则
bpf_u_int32net_mask;//掩码地址
bpf_u_int32net_ip;//网络地址
char*net_interface;//网络接口
constu_char*packet_content;//数据包缓存
u_char*mac_string;//以太网地址
u_shortethernet_type;//以太网类型
structether_header*ethernet_protocol;//以太网协议变量
net_interface=pcap_lookupdev(error_content);//获取网络接口
pcap_lookupnet(//获取网络和掩码地址
net_interface,
&net_ip,
&net_mask,
error_content
);
pcap_handle=pcap_open_live(net_interface,BUFSIZ,1,0,error_content);
//网络接口,数据包大小,混杂模式,等待时间,错误信息
pcap_compile(pcap_handle,&bpf_filter,bpf_filter_string,0,net_ip);
//编译过滤原则
pcap_setfilter(pcap_handle,&bpf_filter);//设置过滤原则
if(pcap_datalink(pcap_handle)!
=DLT_EN10MB)
return;
packet_content=pcap_next(pcap_handle,&protocol_header);//捕获一个网络数据包
printf("从%s捕获了一个数据包\n",net_interface);
printf("数据包长度为:
%d\n",;
ethernet_protocol=(structether_header*)packet_content;
ethernet_type=ntohs(ethernet_protocol->ether_type);//获得以太网类型
printf("以太网类型为:
%04x\n",ethernet_type);
switch(ethernet_type){
case0x0800:
printf("协议类型为IP协议\n");break;
case0x0806:
printf("协议类型为ARP协议\n");break;
case0x8035:
printf("协议类型为RARP协议\n");break;
default:
break;
}
mac_string=ethernet_protocol->ether_shost;//获得源以太网地址
printf("源以太网地址为:
%02x:
%02x:
%02x:
%02x:
%02x:
%02x",*mac_string,*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));
mac_string=ethernet_protocol->ether_dhost;//获得目的以太网地址
printf("目的以太网地址为:
%02x:
%02x:
%02x:
%02x:
%02x:
%02x",*mac_string,*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));
pcap_close(pcap_handle);
}
voidgetarp(){
pcap_t*pcap_handle;
charerror_content[PCAP_ERRBUF_SIZE];
char*net_interface;
structbpf_programbpf_filter;
charbpf_filter_string[]="arp";
bpf_u_int32net_mask;//掩码地址
bpf_u_int32net_ip;//网络地址
pcap_lookupnet(//获取网络和掩码地址
net_interface,
&net_ip,
&net_mask,
error_content
);
structether_header{
u_int8_tether_dhost[8];//目的以太网地址
u_int8_tether_shost[8];//源以太网地址
u_int16_tether_type;//以太网类型
};
typedefu_int32_tin_addr_t;
structarp_header{//arp协议格式
u_int16_tarp_hardware_type;//硬件地址类型
u_int16_tarp_protocol_type;//协议地址类型
u_int8_tarp_hardware_length;//硬件地址长度
u_int8_tarp_protocol_length;//协议地址长度
u_int16_tarp_operation_code;//操作类型
u_int8_tarp_source_ethernet_address[6];//源以太网地址
u_int8_tarp_source_ip_address[4];//源IP地址
u_int8_tarp_destination_ethernet_address[6];//目的以太网地址
u_int8_tarp_destination_ip_address[4];//目的IP地址
};
voidarp_protocol_packet_callback(u_char*argument,conststructpcap_pkthdr*packet_header,constu_char*packet_content){
structarp_header*arp_protocol;//arp协议变量
u_shortprotocol_type;//协议类型
u_shorthardware_type;//硬件类型
u_shortoperation_code;//操作类型
u_char*mac_string;//以太网地址
structin_addrsource_ip_address;//源IP地址
structin_addrdestination_ip_address;//目的I