CAsysncSocket 之--udp工具--AsyncUdpSocket 用法
2013-01-04 20:20
337 查看
============================================================博文原创,转载请声明出处电子咖啡(原id蓝岩)============================================================详解iPhone 下AsyncSocket网络库编程是本文要介绍的内容,iphone的标准推荐CFNetwork C库编程.但是编程比较烦躁。在其它OS往往用类来封装的对Socket函数的处理。比如MFC的CAsysncSocket.在iphone也有类似于开源项目.cocoa AsyncSocket库
官方网站:http://code.google.com/p/cocoaasyncsocket/
这里只上例子,自己在项目中使用的,绝对能使用,如果有疑问,欢迎留言讨论。
将AsyncUdpSocket.h AsyncUdpSocket.m 导入到你的工程中,
在自己建立的util中,加入一下代码
ok,试一下吧
官方网站:http://code.google.com/p/cocoaasyncsocket/
这里只上例子,自己在项目中使用的,绝对能使用,如果有疑问,欢迎留言讨论。
将AsyncUdpSocket.h AsyncUdpSocket.m 导入到你的工程中,
在自己建立的util中,加入一下代码
#pragma mark udp -(void)sendSearchBroadcast{ NSString* bchost=@"255.255.255.255"; //这里发送广播 [self sendToUDPServer:@"hello udp" address:bchost port:BCPORT]; } -(void)sendToUDPServer:(NSString*) msg address:(NSString*)address port:(int)port{ AsyncUdpSocket *udpSocket=[[[AsyncUdpSocket alloc]initWithDelegate:self]autorelease]; //得到udp util NSLog(@"address:%@,port:%d,msg:%@",address,port,msg); //receiveWithTimeout is necessary or you won't receive anything [udpSocket receiveWithTimeout:10 tag:2]; //设置超时10秒 [udpSocket enableBroadcast:YES error:nil]; //如果你发送广播,这里必须先enableBroadcast NSData *data=[msg dataUsingEncoding:NSUTF8StringEncoding]; [udpSocket sendData:data toHost:address port:port withTimeout:10 tag:1]; //发送udp }
//下面是发送的相关回调函数 -(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{ NSString* rData= [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; NSLog(@"onUdpSocket:didReceiveData:---%@",rData); return YES; } -(void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{ NSLog(@"didNotSendDataWithTag----"); } -(void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error{ NSLog(@"didNotReceiveDataWithTag----"); } -(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{ NSLog(@"didSendDataWithTag----"); } -(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{ NSLog(@"onUdpSocketDidClose----"); }
ok,试一下吧
相关文章推荐
- CAsysncSocket 之--udp工具--AsyncUdpSocket 用法
- CAsysncSocket 之--tcp工具--AsyncSocket 用法
- Udp广播的发送和接收(iOS + AsyncUdpSocket)下篇
- 推荐一些socket工具,TCP、UDP调试、抓包工具
- CocoaAsyncSocket UDP发送数据超过包大小限制(Message too long)
- GCDAsyncUdpSocket 组播监听端口接收数据
- QUdpSocket 简单用法
- 利用AsyncUdpSocket实现局域网下的IM(含Demo)
- GCDAsyncUdpSocket 收不到广播包?
- socket UDP TCP 的一些用法
- vs2013 MFC Socket UDP 简单聊天工具
- Swift开发:GCDAsyncUdpSocket 通信之UDP广播
- GCDAsyncUdpSocket添加数据发送超时时间
- socket 异步选择 WSAAsyncSelect 用法
- 关于ios调用第三方asyncUdpSocket闪退的解决方法
- iOS学习笔记4-GCDAsyncUdpSocket的使用(实现异步发送接收数据)
- Socket UDP通信之 AsyncUdpSocket的使用方法及二次封装
- [原创]采用SocketAsyncEventArgs实现异步UDP收发
- 使用 Python 的 Socket 模块构建一个 UDP 扫描工具
- iOS学习笔记5-GCDAsyncUdpSocket中的dispatch_queue_set_specific