目标:在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---------------------\n");
- int i=0;
- for(i=0;i<n_read;i++){
- printf("%.2X ",(unsigned char)eth_head[i]);
- if(((i+1)%16)==0) printf("\n");
- }
- printf("\n----------------------------------------------------\n");
- }
- }
- }
运行效果:
(------------------完---------------------)