GCDAsyncUdpSocket 组播监听端口接收数据
2015-06-09 15:14
651 查看
客户端开发一般是发送数据给服务端。这次反过来了,把客户端作为服务端了,监听对应端口并接收数据.
直接上代码:
.......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:selfdelegateQueue: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
直接上代码:
.......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:selfdelegateQueue: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
相关文章推荐
- jQuery入门
- C++ 智能指针
- 浏览器,tab页显示隐藏的事件监听--页面可见性
- Spring -- framework 各版本源码下载地址
- Spring -- framework 各版本源码下载地址
- Spring -- framework 各版本源码下载地址
- 【PAT 乙级(Basic Level)】数字黑洞
- hibernate执行sql优化
- linux基本命令<2>
- [ 转 ] 一个平庸程序员的自白
- uchome各个功能模块对应模块说明
- Fresco 如何在Android Studio中编译
- 黑马程序员——java反射基础总结
- 高度自适应
- SAT写作全部真题+OG+OC完整版(2)
- 在win7(32)以及xp上python环境的搭建
- python学习之旅(一)-----字典
- 单链表
- 数据透视表中巧用平均值计算及时妥投率
- C#基础-----抽象类实现多态