How to use GCDAsyncUdpSocket for multicast over wifi and bluetooth

℡╲_俬逩灬. 提交于 2019-12-18 13:47:26

问题


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!



来源:https://stackoverflow.com/questions/11621553/how-to-use-gcdasyncudpsocket-for-multicast-over-wifi-and-bluetooth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!