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

该博客介绍了如何在Linux环境下,利用C语言的原始套接字功能,捕获数据链路层上的以太网帧,特别是针对IEC61850-9-2 LE SV类型的电力通信数据包,并进行解析和显示。提供了参考源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

源代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#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--------
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值