通过所有可用网络接口使用GCDAsyncUdpSocket进行UDP广播

时间:2015-09-17 05:19:35

标签: objective-c macos sockets networking gcdasyncudpsocket

我正在使用GCDAsyncUdpSocket来广播UDP数据包以搜索我的NAS设备。

以下是发送和接收UDP数据包的代码段

NSString *broadCastAddress = @"255.255.255.255";
NSInteger udpPort =  8097;

GCDAsyncUdpSocket  *gcdAsyncUdpSocket = [[GCDAsyncUdpSocket alloc]  initWithDelegate:selfdelegateQueue:dispatch_get_main_queue()];
[gcdAsyncUdpSocket bindToPort:udpPort error:&error];
[gcdAsyncUdpSocket setPreferIPv4];

NSData *data = @“Hi there”; // Sample data
[gcdAsyncUdpSocket  enableBroadcast:YES error:&error];
[gcdAsyncUdpSocket  beginReceiving:&error];

[gcdAsyncUdpSocket  sendData:data toHost:broadCastAddress port:udpPort withTimeout:-1 tag:1];

以上代码只能通过单个网络接口发送数据包,即Wifi或以太网或Thunderbolt,我想通过所有可用的网络接口进行广播。 (以太网,WiFi,Thunderbolt等)。

有什么方法可以同时通过所有可用的网络接口(以太网,WiFi,Thunderbolt等)和使用相同的端口进行广播。

提前感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

唷!!经过大量的谷歌搜索和试用后,我找到了解决方案。错误方法。这是解决方案。

  1. 首先使用以下函数枚举所有可用的网络接口。

    - (NSMutableArray *) enumerateAndGetDetailsOfAllNetworkInterfaces
    {
         NSMutableArray *interfaceArray = [[NSMutableArray alloc] init];
         struct ifaddrs *ifap, *ifa;
         struct sockaddr_in *sa;
         char *addr;
    
         getifaddrs (&ifap);
    
          for (ifa = ifap; ifa; ifa = ifa->ifa_next)
          {
           if (ifa->ifa_addr->sa_family==AF_INET)
            {
             QNInterfaceModel *interfaceModel = [[QNInterfaceModel alloc] init];
    
             sa = (struct sockaddr_in *) ifa->ifa_addr;
             addr = inet_ntoa(sa->sin_addr);
             printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, addr);
    
             interfaceModel.interfaceName = [NSString stringWithCString:ifa->ifa_name encoding:NSUTF8StringEncoding];
             interfaceModel.interfaceIPAddress = [NSString stringWithCString:addr encoding:NSUTF8StringEncoding];
    
             [interfaceArray addObject:interfaceModel];
        }
    }
    
      freeifaddrs(ifap);
      return interfaceArray;
    }
    
  2. 我创建了InterfaceModel,它存储了接口名称及其地址。

    步骤2:为每个接口创建一个套接字

        NSMutableArray *interfaceArray = [self enumerateAndGetDetailsOfAllNetworkInterfaces];
    
        for(QNInterfaceModel *interfaceModel in interfaceArray)
        {
           NSError *error;
    
           NSInteger udpPort =  8097;
    
           GCDAsyncUdpSocket *gcdAsyncUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self
                                                                             delegateQueue:dispatch_get_main_queue()];
           [gcdAsyncUdpSocket bindToPort:udpPort interface:interfaceModel.interfaceName error:&error];
    
           gcdAsyncUdpSocket.delegate = self;
    
           if(error == nil)
           {
             [_socketArray addObject:gcdAsyncUdpSocket];
           }
        }
    

    步骤3:将所有创建的套接字存储在一个数组中并按如下方式通过它们进行广播,但在广播之前,我们需要创建一个侦听器套接字来接收响应数据包。

         NSError *error;
    
         NSString *broadCastString = @"255.255.255.255";
    
         NSInteger udpPort =  8097;
         GCDAsyncUdpSocket *listenerGCDAsyncUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self
                                                                      delegateQueue:dispatch_get_main_queue()];
        [listenerGCDAsyncUdpSocket bindToPort:udpPort error:&error];
        [listenerGCDAsyncUdpSocket setPreferIPv4];
        [listenerGCDAsyncUdpSocket  enableBroadcast:YES error:&error];
        [listenerGCDAsyncUdpSocket  beginReceiving:&error];
    
    
       // Send Packets through all the available Interfaces
       for(NSInteger i =0 ; i<_socketArray.count ;i++)
       {
        GCDAsyncUdpSocket *gcdAsyncUdpSocket = [_socketArray objectAtIndex:i];
        gcdAsyncUdpSocket.delegate = self;
    
        if(error)
        {
           // Error connecting
        }
        else
        {
            [gcdAsyncUdpSocket setPreferIPv4];
    
            NSData *data = @"Hi There";
    
            if (![gcdAsyncUdpSocket enableBroadcast:YES error:&error]) {
    
                QNDLog(@"Error enableBroadcast:%@",error);
                return;
            }
    
            [gcdAsyncUdpSocket  beginReceiving:&error];
    
            [gcdAsyncUdpSocket  sendData:data toHost:broadCastString port:udpPort withTimeout:-1 tag:i];
        }
    }