计算机网络实验(共19页).doc
精选优质文档-倾情为你奉上杭 州 电 子 科 技 大 学实 验 报 告学生姓名:韩民杨 学 号: 指导教师:吴端坡实验地点:1#108 实验时间:2015-4-24一、实验室名称:1#108二、实验项目名称:计算机网络实验1Coding on error dectecting algorithms(C+)三、实验学时:四、实验原理:C+编程五、实验目的:利用C+编程CRC16校验及奇偶校验六、实验内容:Coding on error dectecting algorithms(C+)1. Cyclic redundancy checkUsing the polynomials below to encode random generated data stream (40-100bits). Show the FEC, and encoded data frame.CRC-4x4+x+1ITU G.704CRC-16x16+x15+x2+1IBM SDLCCRC-32x32+x26+x23+.+x2+x+1ZIP, RAR, IEEE 802 LAN/FDDI, IEEE 1394, PPP-FCSFor the error patter listed below, what the conclusion does the receiver get? Can the receiver find the errors?CaseError patternNo error00000000One error1000.000Two errors100.001Random errorsRandom error pattern2. Parity check Using even or odd parity check on random generated data stream (8-20bits).Show encoded data frame.For the error patter listed below, what the conclusion does the receiver get? Can the receiver find the errors?CaseError patternNo error00000000One error1000.000Two errors100.001七、实验器材(设备、元器件):PC机一台,装有C+集成开发环境。八、实验步骤:#include<stdio.h>#include<stdlib.h>#include<memory.h>#include<string.h>#include<conio.h> #include<time.h> #define NO_ERROR 1#define ONE_ERROR 2#define TWO_ERROR 3#define RANDOM_ERROR 4#define RESULT 1#define CRC 0#define Parity 0#define check 1int flag_parity;/判断奇偶校验void ByteToBit(char* out,const char* in,int bits);void input_message(char*message);int xor(char *p, char *d,int len);char *crc_function(char *str_p,int len,char *dividend,int len_d,int type,char *crc_p );char Parity_check(char *message_output,int length,int mode);void main()int pattern = 0;int channel_way =0;char message8=NULL;/ data that produced by chance char message_output1000 = NULL;char message_crc = NULL;/transportint length; /message's length/*这是固定了为4为的crc,当这里改变是就可以直接改变crc的长度了*/char dividend="10011"char message_input1000=""/*这里可以更改数值当时16为crc的时候,此时的len_d为15*/int len_d=5;/被除数长度设置char flag;while (true)fflush(stdin);/任意输入数据printf("please input the message!n");gets(message);printf("A need to send this message:%sn",message);length = strlen(message);ByteToBit(message_output,message,length*8);for(int k = 0;k<length*8;k+)printf("%d",message_outputk);message_inputk = message_outputk+48;pattern = 0;channel_way =0;printf("n");printf("Coding on error dectecting algorithmsn");printf(" 1.Cyclic redundancy checkn");printf(" 2.Parity check n");printf(" please chice pattern: n");/*选择进入的检错模式*/scanf("%d",&pattern);fflush(stdin);/清空缓存区if(pattern = 1)printf("*n");printf(" analog channeln");printf(" please choice the way of errorn");printf("* 1.No error *n");printf("* 2.One error *n");printf("* 3.Two errors *n");printf("* 4.Random errors *n");scanf("%d",&channel_way);printf("ncrc:");/进行crc检测int len = length*8;/printf("%dn",len);char *crc = NULL;crc = crc_function(message_input,len,dividend,len_d,CRC,NULL);for(int i = 0;i < len_d;i+)printf("%c",*crc);message_inputlength*8+i = *crc;crc+=1;printf("n");/*模拟信道Case Error patternNo error 00000000One error 1000.000Two errors 100.001Random errorsRandom error pattern*/if(channel_way = NO_ERROR)char *result = NULL;int flagg=0;printf("信道中传输数据:");for(int f =0;f<length*8+len_d-1 ;f+)printf("%c",message_inputf);printf("n");printf("正在检错中.n");printf("检错结果:");fflush(stdin);result = crc_function(message_input,len,dividend,len_d,RESULT,crc-len_d);for(int i = 0;i < len_d-1;i+)printf("%c",*result);if(*result) - 48 != 0)printf("n error! n");flagg =1;break;result+=1;if(flagg = 0)printf("n no error! n");if(channel_way = ONE_ERROR)char *result = NULL;message_input0 = (message_input0 - 48)('1'-48) + 48;printf("信道中传输数据:");for(int f =0;f<length*8+len_d-1 ;f+)printf("%c",message_inputf);printf("n");printf("正在检错中.");printf("检错结果:");result = crc_function(message_input,len,dividend,len_d,RESULT,crc-len_d);for(int i = 0;i < len_d-1;i+)if(*result = '1')printf("data error! n");break;elsecontinue;result+=1;if(channel_way = TWO_ERROR)char *result = NULL;message_input0 = (message_input0 - 48)('1'-48) + 48;message_inputlength*8-1 = (message_inputlength*8-1 - 48)('1'-48) + 48;printf("信道中传输数据:");for(int f =0;f<length*8+len_d-1 ;f+)printf("%c",message_inputf);printf("n");printf("正在检错中.");printf("检错结果:");result = crc_function(message_input,len,dividend,len_d,RESULT,crc);for(int i = 0;i < len_d-1;i+)if(*result != '0')printf(" data error! n");break;elsecontinue;result+=1;if(channel_way = RANDOM_ERROR)char *result = NULL;int no;srand( (unsigned)time( NULL ) ); /初始化随机数no = rand()%length*8;message_inputno = (message_input0 - 48)('1'-48) + 48;printf("信道中传输数据:");for(int f =0;f<length*8+len_d-1 ;f+)printf("%c",message_inputf);printf("n");printf("正在检错中.n");printf("检错结果:");result = crc_function(message_input,len,dividend,len_d,RESULT,crc);for(int i = 0;i < len_d-1;i+)if(*result != '0')printf(" data error! n");break;elsecontinue;result+=1;printf("*n");printf("n");printf("n");elseprintf("Please select the parity moden");printf(" 1.Even parity n");printf(" 2.Odd parityn");scanf("%d",&flag_parity);char m = Parity_check(message_input,length*8,Parity);printf("在信道中传输的message:");message_inputlength*8 = m ;for(int message_number = 0;message_number<=length*8;message_number+)printf("%c",message_inputmessage_number);printf("n");printf("*n");printf("* analog channel *n");printf("*please choice the way of error*n");printf("* 1.No error *n");printf("* 2.One error *n");printf("* 3.Two errors *n");scanf("%d",&channel_way);if(channel_way = NO_ERROR)for(int f =0;f<length*8;f+)printf("%c",message_inputf);printf("正在检错中.n");printf("检错结果:");char flag_parity_mode = Parity_check(message_input,length*8,check);if(flag_parity_mode = (message_inputlength*8)printf("no error!n");if(channel_way = ONE_ERROR)message_input0 = (message_input0 - 48)('1'-48) + 48;for(int f =0;f<length*8;f+)printf("%c",message_inputf);printf("正在检错中.n");printf("检错结果:");char flag_parity_mode = Parity_check(message_input,length*8,check);printf(" error!n");if(channel_way = TWO_ERROR)message_input0 = (message_input0 - 48)('1'-48) + 48;message_inputlength*8-1 = (message_inputlength*8-1 - 48)('1'-48) + 48;for(int f =0;f<length*8;f+)printf("%c",message_inputf);printf("正在检错中.n");printf("检错结果:");char flag_parity_mode = Parity_check(message_input,length*8,check);printf("no error!n");printf("*n");printf("n");printf("n");fflush(stdin);printf("do you want to continue?y/nn");scanf("%c",&flag);if(flag = 'y')continue;elsebreak;/*/function: 完成字节转换成位的功能/输入字符:In 输出的二进制:Out*/void ByteToBit(char* out,const char* in,int bits)/char out_11000 = ""int i;/printf("%sn",in);for(i=0;i<bits;+i)outbits-i-1=(ini>>3>>(i&7)&1;/printf("%d:%dn",i,outi);/*/function: 完成异或除法/输入:*p *d /输出:直接将一次异或除法之后的结果覆盖*/int xor(char *p, char *d,int len=5)/p为message,d 为crc检测的中的pint i=0;for(;i<len;i+)*p=(*p-48)(*d-48)+48;*p=*p+1;d=d+1;d=d-len; /d重新指向最新的/*p=*p-len+1;/p后移一位return 0;/*/function: 完成crc检错以及crc计算/输入:传输字符:str_p 传输字符长度len / 4位的crc的检错方式 len_d是需要填0的个数/ type 用于判断显示计算传输之后的结果还是计算crc/输出:crc或者计算传输之后的结果*/char *crc_function(char *str_p,int len,char *dividend,int len_d,int type,char *crc_p=NULL)int i;int j;char *input_p=(char *)malloc(sizeof(char)*(len+len_d-1); /申请空间char *input_p_tmp=NULL;input_p_tmp=input_p;/向指针指向的内存里存数据for(i=0;i<len;i+)*input_p_tmp=str_pi;input_p_tmp+;/*补零个数为len_d-1*/for(i=0;i<len_d-1;i+)if(type=CRC)/如果是求crc就补0*input_p_tmp='0'input_p_tmp+;else/如果是求结果就补crc*input_p_tmp=*crc_p;crc_p+;input_p_tmp+;input_p_tmp=NULL;/开始检查如果是1则进行异或除法,如果是0就指针向下移动一位.其中'0'=48for(j=0;j<len;j+)/*把1传入48表示为"0"*/if(*input_p=48)/指针后移一位input_p=input_p+1;elsexor(&input_p,dividend,len_d);return input_p;/*/function: 完成奇偶校验/输入:message_output为需要传输的数据以及数据的长度lengh 奇偶校验的模式/输出:返回奇偶校验的结果*/char Parity_check(char *message_output,int length,int mode)fflush(stdin);int j = 0;/偶校验if(flag_parity= 1)if(mode = Parity)for(int i = 0;i<length;i+)if(message_outputi = 49)j+;elsecontinue;if(j%2 = 0)return '0'elsereturn '1'elsefor(int i = 0;i<length;i+)if(message_outputi = 49)j+;elsecontinue;if(j%2 = 0)return '0'elsereturn '1'/奇校验else if(flag_parity = 0)if(mode = Parity)for(int i = 0;i<length;i+)if(message_outputi = 49)j+;elsecontinue;if(j%2 = 1)return '0'elsereturn '1'elsefor(int i = 0;i<length;i+)if(message_outputi = 49)j+;elsecontinue;if(j%2 = 1)return '0'elsereturn '1'九、实验数据及结果分析:1.输入界面2.输入数据3. CRC校验1)No error2)One error3)Two errors4)Random errors4. 奇偶校验界面Even parity1)No error2)One error3)Two errorsOdd parity1)No error2)One error3)Two errors十、实验结论:奇偶校验,1位错可发现,2位错不可发现(奇数个错可发现,偶数个错不可发现)。而CRC校验,奇数或偶数个错均能发现。说明CRC校验比奇偶校验好的多。十一、总结及心得体会:通过本次实验,加深了C+的学习,虽然是结合网上的一些资料,但还是圆满完成,上次叫的作业写的不是很认真,望老师见谅,并且了解了CRC与奇偶校验的工作特性。专心-专注-专业