如何使用 GCDAsyncUdpSocket 通过 wifi 和蓝牙进行多播 [英] How to use GCDAsyncUdpSocket for multicast over wifi and bluetooth

查看:25
本文介绍了如何使用 GCDAsyncUdpSocket 通过 wifi 和蓝牙进行多播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 GCDAsyncUdpSocket 通过 wifi 在 iOS 设备之间发送多播数据报.

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

代码很简单..

客户

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];

服务器

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];

这在 wifi 上运行良好.我如何让它也通过蓝牙工作?我用谷歌搜索了一堆,找不到任何东西......我需要创建两个单独的套接字吗?一个绑定到wifi接口,另一个绑定到蓝牙接口?

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?

还是我对一些基本的东西感到困惑?这一定是可能的.GameKit 的 GKSession 正是这样做的,对吧?

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

推荐答案

你们很亲近.您可以在这个 StackOverflow 问题中找到有用信息的指针,或者您可以使用包装此功能的库,例如 HHServices.

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!

这篇关于如何使用 GCDAsyncUdpSocket 通过 wifi 和蓝牙进行多播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆