您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

从Linux内核发送UDP数据包

从Linux内核发送UDP数据包

几个月前我解决了我的问题。这是我使用的解决方案。

标准的包发送API(sock_create,connect等)不能在少数情况下使用(中断)。在错误的地方使用它会导致KP。

netpoll API更“底层”,并且可以在每种情况下使用。但是,有几个条件:

确保尊重他们,因为如果出现问题,您将不会收到任何错误消息。它只会地失败:)这是一些代码

宣言

#include <linux/netpoll.h>
#define MESSAGE_SIZE 1024
#define INADDR_LOCAL ((unsigned long int)0xc0a80a54) //192.168.10.84
#define INADDR_SEND ((unsigned long int)0xc0a80a55) //192.168.10.85
static struct netpoll* np = NULL;
static struct netpoll np_t;

初始化

np_t.name = "LRNG";
strlcpy(np_t.dev_name, "eth0", IFNAMSIZ);
np_t.local_ip = htonl(INADDR_LOCAL);
np_t.remote_ip = htonl(INADDR_SEND);
np_t.local_port = 6665;
np_t.remote_port = 6666;
memset(np_t.remote_mac, 0xff, ETH_ALEN);
netpoll_print_options(&np_t);
netpoll_setup(&np_t);
np = &np_t;

采用

char message[MESSAGE_SIZE];
sprintf(message,"%d\n",42);
int len = strlen(message);
netpoll_send_udp(np,message,len);

希望它可以帮助某人。

其他 2022/1/1 18:19:45 有566人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶