C++实现发送和接收自定义类型的链路层数据帧——绑定,目的地址同于接收网卡

一 实战前准备

1 准备两台虚拟机

A机配置

网卡

IP

MAC

enp0s3

192.168.0.110

08:00:27:60:7b:7f

enp0s8

192.168.0.104

08:00:27:87:45:35

B机配置

网卡

IP

MAC

enp0s3

192.168.0.120

08:00:27:0c:3b:d3

二 接收端

1 代码

#include <stdio.h>  
#include <string.h>  
#include <errno.h>  
#include <sys/types.h>   
#include <sys/socket.h>  
#include <netpacket/packet.h>  
#include <net/if.h>  
#include <net/if_arp.h>  
#include <sys/ioctl.h>  
#include <arpa/inet.h> //for htons
#include <netinet/if_ether.h>   //for ethhdr
#define LEN     60
void print_str16(unsigned char buf[], size_t len)  
{  
    int     i;  
    unsigned char   c;  
    if (buf == NULL || len <= 0)  
        return;  
    for (i = 0; i < len; i++) {  
        c = buf[i];  
        printf("%02x", c);  
    }  
    printf("\n");  
}  
void print_sockaddr_ll(struct sockaddr_ll *sa)  
{  
    if (sa == NULL)  
        return;  
    printf("sll_family:%d\n", sa->sll_family);  
    printf("sll_protocol:%#x\n", ntohs(sa->sll_protocol));  
    printf("sll_ifindex:%#x\n", sa->sll_ifindex);  
    printf("sll_hatype:%d\n", sa->sll_hatype);  
    printf("sll_pkttype:%d\n", sa->sll_pkttype);  
    printf(
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值