How to use GCDAsyncUdpSocket for multicast over wi

2019-02-07 13:59发布

问题:

I am currently using GCDAsyncUdpSocket to send multicast datagrams over wifi between iOS devices.

The Code is pretty simple..

Client

self.socket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self  delegateQueue:dispatch_get_main_queue()];

//omitted error checking
[self.socket bindToPort:12345 error:&err];
[self.socket joinMulticastGroup:@"224.0.1.1" error:&err];
[self.socket beginReceiving:&err];

Server

self.multicastSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

NSData *d = [@"hello" dataUsingEncoding:NSUTF8StringEncoding];

[self.multicastSocket sendData:d toHost:@"224.0.1.1" port:12345 withTimeout:-1 tag:11];

This works well over wifi. How do I make it work over bluetooth as well? I have googled a bunch and can't find anything... Do I need to create two separate sockets? One bound to the wifi interface and another to the bluetooth interface?

EDIT: or am I confused about something fundamental? This must be possible. GameKit's GKSession does exactly this, right?

回答1:

I think you are missing the BT stack access which enables BT over ip protocol. Also, multicast relies on a network device (router) to manage these groups and registrations. BT requires a concept of ZeroConf networks (which can also work for wifi), but is managed in a peer to peer manner. Have a look at Bonjour or Dnssd for an implementation closer to the socket level than GameKit.



回答2:

You are very close. You can find pointers to useful information in this StackOverflow question or you can use a library such as HHServices that wraps this functionality.

I honestly have tried this route with little luck, but I hope this points you in the right direction. Let me know if you've figured it out!