Ethernet帧结构解析程序C++
#include"stdafx.h"
#include
#include
#include
using namespace std;
const int MAXLENGTH = 1500;
const int MINLENGTH = 46;
const char destination[] =
{
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);
//计算封帧个数
int truelen = data.length(); //数据的真实长度
int length = data.length();
int frameTotal = 0; //总的帧的个数
if (length>MAXLENGTH)
{
frameTotal = length / MAXLENGTH;
if (length%MAXLENGTH)
frameTotal++;
cout << "数据字段大于1500B,将封装成" << frameTotal << "个帧……" << endl;
}
else if (length { while (length { 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 { frameTotal = 1; cout << "数据字段介于46B和1500B之间,将封装成1个帧……" << endl; } else { frameTotal = 1; cout << "数据字段等于1500B,将封装成1个帧……" << endl; } cout << endl; //cout << truelen << endl; //cout << length << endl; 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); //将文件指针移到文件末尾 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校验的位置 outfile.write(destination, sizeof(destination)); //写入6字节的目的地址 outfile << endl; outfile.write(source, sizeof(source)); //写入6字节的源地址 outfile << endl; if (truelen >= MAXLENGTH) { 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; 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; } streampos nCrc = outfile.tellp(); //获得帧校验字段的位置 outfile.put(char(0x00)); //数据后补1字节的0,用于CRC计算 int total = nCrc - nCrcs; //获得需要进行CRC计算的数据长度 outfile.seekg(nCrcs, ios::beg); //将文件指针指向目的地址 unsigned char crc = 0; //初始化CRC余数为0 while (total--) { char temp; outfile.get(temp); for (unsigned char i = (unsigned char)0x80; i>0; i >>= 1) { 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; cout << "第" << i << "个帧封装完毕!" << endl; cout << endl; outfile.close(); } infile.close(); return 0; }