gpt4 book ai didi

c++ - iPhone 在个人热点模式下不发送网络广播/多播

转载 作者:可可西里 更新时间:2023-11-01 03:29:28 27 4
gpt4 key购买 nike

根据最近的经验发现和网络上的各种帖子,在启用了个人热点的 iPhone 上运行的应用程序似乎无法将广播和/或多播发送到个人热点的网络上。任何人都可以阐明这个问题的原因吗?

应用

我有一个 IOS 应用程序,它是用跨平台 C++ 代码构建的,可以将其存在广播和多播到它运行的网络上。当 iPhone 连接到 Wi-Fi 网络时,该应用程序可以完美运行。在这种情况下,网络上的其他设备接收广播/多播,并且一切正常。这可以通过将运行 WireShark 的计算机连接到网络来轻松验证——可以在数据包跟踪中看到广播/多播数据包。

不用说,该应用程序在连接到本地 Wi-Fi 的 iPhone 上运行良好。

问题

当我在启用了个人热点的 iPhone 上运行该应用程序时,没有广播/多播被发布到热点网络上。这可以使用 WireShark 进行验证,它在其跟踪中没有显示此类数据包。

将个人热点用作能够处理广播和多播的网络路由器是否有任何限制?

当我使用浏览器在“WireSharking”设备上请求网页时,个人热点正确响应所有数据包,返回网页内容。

附带信息

我遇到过其他报告相同或类似问题的 Stack Overflow 帖子:

  1. TCP connection not working properly when using iPhone as hotspot
  2. Fail to send ssdp broadcast by personal hotspot

Michael Tyson 的“The Making of Talkie: Multi-interface broadcasting and multicast”是在 iPhone 上编写此类广播/多播应用程序的一个很好的教程。足以说明我的应用程序符合所有要求(例如,在适当的地方设置套接字选项 SO_BROADCAST、SO_DONTROUTE 和 IP_MULTICAST_IF)。

上面引用文献(1)的回复写道“难道是因为个人热点引入了网络地址转换?”。我过滤了 WireShark 跟踪以仅显示连接到热点 IP 的数据包,没有证据表明个人热点向 NAT 地址发送任何内容。

总结

谁能解释为什么运行个人热点的 iPhone 没有广播/多播数据包,以及如何解决这个问题?

非常感谢。

最佳答案

我开始广播了(在 iOS 10 上)

我发布了第二个答案,因为我找到了一种在个人热点模式下在 iOS 10 上进行广播的方法。解决方案有点复杂,但这是我如何让它工作的:

  1. 使用 getifaddrs(if) 循环遍历所有接口(interface)(do {} while (if = if->ifa_next))
  2. 拒绝环回接口(interface)(if->ifa_flags & IFF_LOOPBACK)
  3. 仅过滤支持广播的接口(interface)(if->ifa_flags & IFF_BROADCAST)
  4. 使用 int sock = socket() 创建套接字
  5. 启用广播:int yes = 1; setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &yes, sizeof yes)
  6. 连接到广播地址:connect(sock, if->ifa_dstaddr, if->ifa_dstaddr->sa_len)
  7. 现在使用send() 发送您的消息!

我已经确认这在 iPhone 连接到 WiFi 网络时以及在个人热点模式下都有效。

完整示例代码

#include <ifaddrs.h>
#include <arpa/inet.h>
#include <net/if.h>

-(IBAction)sayHello:(id)sender {

// fetch a linked list of all network interfaces
struct ifaddrs *interfaces;
if (getifaddrs(&interfaces) == -1) {
NSLog(@"getifaddrs() failed: %s", strerror(errno));
return;
}

// loop through the linked list
for(struct ifaddrs *interface=interfaces; interface; interface=interface->ifa_next) {

// ignore loopback interfaces
if (interface->ifa_flags & IFF_LOOPBACK) continue;

// ignore interfaces that don't have a broadcast address
if (!(interface->ifa_flags & IFF_BROADCAST) || interface->ifa_dstaddr == NULL) continue;

// check the type of the address (IPv4, IPv6)
int protocol_family;
struct sockaddr_in ipv4_addr = {0};
struct sockaddr_in6 ipv6_addr = {0};
struct sockaddr *addr;
if (interface->ifa_dstaddr->sa_family == AF_INET) {
if (interface->ifa_dstaddr->sa_len > sizeof ipv4_addr) {
NSLog(@"Address too big");
continue;
}
protocol_family = PF_INET;
memcpy(&ipv4_addr, interface->ifa_dstaddr, interface->ifa_dstaddr->sa_len);
ipv4_addr.sin_port = htons(16000);
addr = (struct sockaddr *)&ipv4_addr;

char text_addr[255] = {0};
inet_ntop(AF_INET, &ipv4_addr.sin_addr, text_addr, sizeof text_addr);
NSLog(@"Sending message to %s:%d", text_addr, ntohs(ipv4_addr.sin_port));
}
else if (interface->ifa_dstaddr->sa_family == AF_INET6) {
if (interface->ifa_dstaddr->sa_len > sizeof ipv6_addr) {
NSLog(@"Address too big");
continue;
}
protocol_family = PF_INET6;
memcpy(&ipv6_addr, interface->ifa_dstaddr, interface->ifa_dstaddr->sa_len);
ipv6_addr.sin6_port = htons(16000);
addr = (struct sockaddr *)&ipv6_addr;

char text_addr[255] = {0};
inet_ntop(AF_INET6, &ipv6_addr.sin6_addr, text_addr, sizeof text_addr);
NSLog(@"Sending message to %s:%d", text_addr, ntohs(ipv6_addr.sin6_port));
}
else {
NSLog(@"Unsupported protocol: %d", interface->ifa_dstaddr->sa_family);
continue;
}

// create a socket
int sock = socket(protocol_family, SOCK_DGRAM, IPPROTO_UDP);
if (sock == -1) {
NSLog(@"socket() failed: %s", strerror(errno));
continue;
}

// configure the socket for broadcast mode
int yes = 1;
if (-1 == setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &yes, sizeof yes)) {
NSLog(@"setsockopt() failed: %s", strerror(errno));
}

// create some bytes to send
NSString *message = @"Hello world!\n";
NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];

if (-1 == connect(sock, addr, addr->sa_len)) {
NSLog(@"connect() failed: %s", strerror(errno));
}

// send the message
ssize_t sent_bytes = send(sock, data.bytes, data.length, 0);
if (sent_bytes == -1) {
NSLog(@"send() failed: %s", strerror(errno));
}
else if (sent_bytes<data.length) {
NSLog(@"send() sent only %d of %d bytes", (int)sent_bytes, (int)data.length);
}

// close the socket! important! there is only a finite number of file descriptors available!
if (-1 == close(sock)) {
NSLog(@"close() failed: %s", strerror(errno));
}
}

freeifaddrs(interfaces);
}

此示例方法在端口 16000 上广播 UDP 消息。

对于调试,您可以使用工具socat。只需在您的计算机(应与手机处于同一网络)上运行以下命令即可:

socat UDP-RECV:16000 STDOUT

关于c++ - iPhone 在个人热点模式下不发送网络广播/多播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28795808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com