Ethernet帧结构解析程序C++
- 格式:pdf
- 大小:91.13 KB
- 文档页数:5
frameTotal = length / MAXLENGTH;
streampos nCrc = outfile.tellp(); //获得帧校验字段的位 置
outfile.put(char(0x00)); //数据后补1字节的0,用于CRC计 算
int total = nCrc - nCrcs; //获得需要进行CRC计算的数据长 度
outfile.seekg(nCrcs, ios::beg); //将文件指针指向目的地 址
cout << "数据字段:" << divideStr << endl; leftStr = leftStr.substr(MAXLENGTH, length); truelen = leftStr.length(); } else { outfile.put(char(truelen / 256)); //写入2字节的长度 字段 outfile.put(char(truelen % 256)); outfile << leftStr << endl; //写入数据字段 cout << "长度字段:" << hex << truelen << dec << " (" << truelen << ")" << endl; cout << "数据字段:" << leftStr << endl; }
divideStr = leftStr.substr(0, MAXLENGTH); outfile.put(char(MAXLENGTH / 256)); //写入2字节的长 度字段 outfile.put(char(MAXLENGTH % 256)); outfile << divideStr << endl; //写入数据字段 cout << "长度字段:" << hex << MAXLENGTH << dec << "(" << MAXLENGTH<< ")" << endl;
data += '0'; //cout << data << endl; length++; //cout << data.length() << endl; } frameTotal = 1; cout << "数据字段小于46B,将封装成1个帧……" << endl; } else if (length == MINLENGTH) { frameTotal = 1; cout << "数据字段等于46B,将封装成1个帧……" << endl; } else if (length>MINLENGTH&&length<MAXLENGTH) { frameTotal = 1; cout << "数据字段介于46B和1500B之间,将封装成1个帧……" << endl; } else { frameTotal = 1; cout << "数据字段等于1500B,将封装成1个帧……" << endl; } cout << endl; //cout << truelen << endl; //cout << length << endl;
if (length%MAXLENGTH) frameTotal++;
cout << "数据字段大于1500B,将封装成" << frameTotal << "个帧……" << endl;
} else if (length<MINLENGTH) {
while (length<MINLENGTH) {
unsigned char crc = 0; //初始化CRC余数为0 while (total--) {
char temp; outfil1)
for (unsigned char i = (unsigned char)0x80; i>0; i
{ if (crc & 0x80) { crc <<= 1; if (temp) crc ^= 0x01;
crc ^= 0x70; } else {
crc <<= 1; if (temp&i)
crc ^= 0x01; } } } outfile.seekp(nCrc, ios::beg); //将文件写指针移到数据字 段后 outfile << crc << endl; //将CRC余数写入帧校验字段 cout << "帧校验字段:" << hex << (int)crc << dec << "(" << (int)crc << ")" << endl;
for (int j = 0; j<7; j++) //写入7字节的前导码 outfile.put((char)0xaa);
outfile << endl;
outfile.put((char)0xab); //写入1字节的帧前定界符 outfile << endl;
streampos nCrcs = outfile.tellp(); //获得开始CRC校验的 位置
string divideStr; //本次拆分好的数据 string leftStr = data; //剩余未封装的数据 for (int i = 1; i <= frameTotal; i++) {
fstream outfile; char filename[10]; sprintf_s(filename,"%d.txt",i); outfile.open(filename, ios::out | ios::binary | ios::trunc); outfile.seekg(0, ios::end); //将文件指针移到文件末尾
outfile.write(destination, sizeof(destination)); //写入 6字节的目的地址
outfile << endl;
outfile.write(source, sizeof(source)); //写入6字节的源 地址
outfile << endl;
if (truelen >= MAXLENGTH) {
char(0xFF), char(0xFF), char(0xFF), char(0xFF), char(0xFF), char(0xFF) }; const char source[] = { char(0x00), char(0x16), char(0x76), char(0xB4), char(0xE4), char(0x77) };
int main() {
fstream infile; //fstream outfile; string data; //outfile.open("FrameParse.txt", ios::out|ios::binary|ios::trunc); infile.open("南开简介.txt"); getline(infile, data);
#include "stdafx.h" #include <iostream> #include <fstream> #include <string>
using namespace std;
const int MAXLENGTH = 1500; const int MINLENGTH = 46; const char destination[] = {
cout << "第" << i << "个帧封装完毕!" << endl; cout << endl; outfile.close(); }
infile.close(); return 0; }