简体   繁体   中英

How to know target address and port when receiving a UDP packet with libuv?

uvlib UDP accepts event callback function of the following type:

typedef void (*uv_udp_recv_cb)(uv_udp_t* handle,
                               ssize_t nread,
                               const uv_buf_t* buf,
                               const struct sockaddr* addr,
                               unsigned flags);

There is no info about target port and address like sent_to_addr . Is there way to achieve it?

I need this to know on what interface packet received, or to know multicast group. Socket listens on 0.0.0.0:xxxx

IP_PKTINFO具有此类信息,但是libuv没有公开启用它的API。

Yes, @Inaki right. I found issues in node.js repository. They are active for years. This feature request is still unresolved because of portability problems.

There are workarounds:

  1. for multicast -> bind to multicast address.
  2. for unicast -> bind to concrete local interface, 1 interface - 1 socket. But keep in mind going this way dynamic addresses could become pitfalls

快速搜索显示了一个类似的问题: 获取接收到的UDP数据包的目标地址您可以直接调用系统的API来获取信息,而无需绑定到特定地址。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM