使用Linux 原始套接字抓取数据链路层上IEC61850-9-2(LE) SV数据包并显示的参考程序

目标:在linux下使用C语言的原始套接字来接收以太网数据链路层上的数据,如果接收的数据是IEC61850-9-2 SV类型,则打印。。。。仅供参考!

源代码:

#include 
#include 
#include 
#include 
#include 
#include 
#define BUFFER_MAX 2048

int main(int argc, char *argv[])
{      
int sock, n_read, eth_type;        
char buffer[BUFFER_MAX];
char  *eth_head;
       
if((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
{
	fprintf(stdout, "create socket error/n");
        exit(0);
}
        
while(1) 
{
	n_read = recvfrom(sock, buffer, 2048, 0, NULL, NULL);
   	if(n_read < 42) 
	{
		fprintf(stdout, "Incomplete header, packet corrupt/n");
		continue;
	}
               
	eth_head = buffer;
	eth_type=((unsigned char)eth_head[16])*16*16+(unsigned char)eth_head[17];
	
	if(eth_type==0x88ba){    //judge wether the eth_type is iec61850 sv
		printf("\n----------------IEC61850-9-2 SV---------------------\n");
        	int i=0;
		for(i=0;i

运行效果:

使用Linux 原始套接字抓取数据链路层上IEC61850-9-2(LE) SV数据包并显示的参考程序_第1张图片

(------------------完---------------------)

你可能感兴趣的:(C语言,嵌入式相关)