GCDAsyncUdpSocket 组播监听端口接收数据
来源:互联网 发布:百度云备案域名出售 编辑:程序博客网 时间:2024/06/07 13:50
客户端开发一般是发送数据给服务端。这次反过来了,把客户端作为服务端了,监听对应端口并接收数据.
直接上代码:
.......h文件
@interface ListenServerData : NSObject {
}
- (id)initWithData;
@end
........m文件#import "ListenServerData.h"
#import "GCDAsyncUdpSocket.h"
@interface ListenServerData () {
}
@property (strong, nonatomic) GCDAsyncUdpSocket *gcdUdpSocket;
@end
@implementation ListenServerData
- (id)initWithData;
{
if (self = [super init]) {
self.gcdUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error;
[_gcdUdpSocket bindToPort:88898 error:&error];
if (nil != error) {
NSLog(@"failed.:%@",[error description]);
}
[_gcdUdpSocket enableBroadcast:YES error:&error];
if (nil != error) {
NSLog(@"failed.:%@",[error description]);
}
//组播224.0.0.2地址,如果地址大于224的话,就要设置GCDAsyncUdpSocket的TTL(默认TTL为1)
[_gcdUdpSocket joinMulticastGroup:@"224.0.0.2" error:&error];
if (nil != error) {
NSLog(@"failed.:%@",[error description]);
}
[_gcdUdpSocket beginReceiving:&error];
if (nil != error) {
NSLog(@"failed.:%@",[error description]);
}
}
return self;
}
- (void)dealloc
{
if (_gcdUdpSocket) {
[_gcdUdpSocket close];
}
}
#pragma mark -GCDAsyncUdpsocket Delegate
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext
{
NSLog(@"Reciv Data len:%d",[data length]);
}
- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error
{
NSLog(@"udpSocketDidClose Error:%@",[error description]);
}
@end
- GCDAsyncUdpSocket 组播监听端口接收数据
- GCDAsyncUdpSocket 组播监听端口接收数据
- iOS GCDAsyncUdpSocket UDP 建立连接,实现组播
- iOS学习笔记4-GCDAsyncUdpSocket的使用(实现异步发送接收数据)
- 组播接收
- UDP组播接收
- Linux下发送与接收组播数据(socket编程)
- Linux下发送与接收组播数据(socket编程)
- Linux下发送与接收组播数据(socket编程)
- UDP端口传递接收数据
- 组播发送接收过程
- linux 组播接收注意事项
- VLC组播与接收
- VLC组播与接收
- VLC组播与接收
- GCDAsyncUdpSocket添加数据发送超时时间
- Socket 进行UDP广播数据(GCDAsyncUdpSocket)
- Socket 进行UDP广播数据(GCDAsyncUdpSocket)
- OpenGL ES之glUniform函数
- 让SD卡转接成SSD固态硬盘
- js data 格式转换
- Scripts:报告所有用户session信息的脚本sess_user_sessions.sql
- php中比较两个数组内容,相同部分保留,不同的加上
- GCDAsyncUdpSocket 组播监听端口接收数据
- 位图与文字
- 校赛 SDUT OJ2860生日Party(BFS)
- 第十二章 面向语言编程
- 蔡文胜给创业者的10条建议
- 泛型
- Scripts:查出用户有未提交事务的脚本sess_uncommited_transactions.sql
- ini配置文件常用的四个函数
- 获取请求的客户端的 IP