基于udp socket 通讯
2016-02-16 15:59
183 查看
//首先建立基于UDP的Socket连接 AsyncUdpSocket *updSocket -(void)openUDPServer{ //初始化udp AsyncUdpSocket *tempSocket=[[AsyncUdpSocket alloc] initWithDelegate:self]; self.updSocket=tempSocket; [tempSocket release]; //绑定端口 NSError *error = nil; [self.updSocket bindToPort:4333 error:&error]; [self.updSocket joinMulticastGroup:@"224.0.0.1" error:&error]; //启动接收线程 [self.updSocket receiveWithTimeout:-1 tag:0]; } //连接建好后处理相应send Events -(void)sendMessage:(NSString*)message { NSMutableString *sendString = [NSMutableString stringWithCapacity:100]; [sendString appendString:message]; //开始发送 BOOL res = [self.updSocket sendData:[sendString dataUsingEncoding:NSUTF8StringEncoding] toHost:@"224.0.0.1" port:4333 withTimeout:-1 tag:0]; NSLog(@"%@",sendString); if(res) { NSLog(@"xxxxffff"); } else { NSLog(@"error"); } } -(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port { NSLog(@"onUdpSocket successful"); //已经处理完毕 do something return YES; } -(void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error { //无法发送时,返回的异常提示信息 do something NSLog(@"error1"); } -(void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error { //无法接收时,返回异常提示信息 do something NSLog(@"error2"); }
相关文章推荐
- yii ajax 分页
- Java RMI之HelloWorld篇
- 线性表之双链表--DoublyLinkList,类似于类库中的 LinkedList
- Linux学习笔记--常用目录及其作用
- JAVA 生成uuid
- linux 下PHP使用多语言
- 渗透入侵思路
- input框颜色修该
- linux中source、bash命令区别
- photoshop的页面图的长度与宽度获取
- solr跨core查询
- 飞鸽传书原理以及相关代码详解
- IOS学习 UIView 页面布局
- 空包的签名方法
- http 错误代码表
- tomcat源码编译与导入eclipse
- openfire在windows环境和linux环境下的配置
- KVC,KVO,通知
- Android事件分发机制学习和总结
- Windows Azure Application Gateway 应用程序网关