当前位置:首页 > 办公文档 > 其它办公文档 > 计算机网络课程设计:(第三章)封装Ethernet帧(完整程序代码)
计算机网络课程设计第二章:封装Ethernet帧完整程序:#includeiostream.h#includefstream.hvoidmain(intargc,char*argv[]){if(argc!=3){coutPleaseinputcommmand:EncapFrameinput_fileoutput_fileendl;return;}fstreamfile;file.open(argv[2],ios::in|ios::out|ios::binary|ios::trunc);for(inti=0;i7;i++)file.put(char(0xaa));file.put(char(0xab));longpCrcS=file.tellp();chardst_addr[6]={char(0x00),char(0x00),char(0xe4),char(0x86),char(0x3a),char(0xdc)};file.write(dst_addr,sizeof(dst_addr));charsrc_addr[6]={char(0x00),char(0x00),char(0x80),char(0x1a),char(0xe6),char(0x65)};file.write(src_addr,sizeof(src_addr));ifstreaminfile;infile.open(argv[1],ios::binary);infile.seekg(0,ios::end);shortlength=(short)infile.tellg();file.put(char(length/256));file.put(char(length%256));char*data=newchar[length];infile.seekg(0,ios::beg);infile.read(data,length);file.write(data,length);infile.close();deletedata;if(length46)for(inti=0;i46-length;i++)file.put(char(0x00));longpCrc=file.tellp();file.put(char(0x00));shorttotal=short(file.tellp())-(short)pCrcS;file.seekg(pCrcS,ios::beg);unsignedcharcrc=0;while(total--){unsignedchartemp;file.get(temp);for(unsignedchari=(unsignedchar)0x80;i0;i=1){if(crc&0x80){crc=1;if(temp&i)crc^=0x01;crc^=0x07;}else{crc=1;if(temp&i)crc^=0x01;}}}file.seekp(pCrc,ios::beg);file.put(crc);file.seekg(0,ios::beg);coutContentofethernetframe:endl;while(!file.eof()){unsignedchartemp;file.get(temp);couthex(short)temp;}coutendlTheargv[2]iscompleted;file.close();}结果:
本文标题:计算机网络课程设计:(第三章)封装Ethernet帧(完整程序代码)
链接地址:https://www.777doc.com/doc-5838442 .html