关于iOS udp 通信的问题 说明一下:[self scheduleDequeueReceive];和- (void)scheduleDequeueReceive方法都是AsyncUdpSocket.m文件中的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port里面的代码贴出来看看再说。你那个错误是使用了已经被释放的内存。别人的库不存在这样的问题,你没改里面的代码的话,就不用把别人库里面的代码帖出来了。 -(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{ NSLog(@"received data: %@",[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]); NSLog(@"%@",host); if(tag==1) { if([[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease] compare:@"ihd"]) { hostIP=[[NSString alloc] initWithString:host]; //[udpSocket close]; } } if(tag==sn) { //先测试是否能够获取到指定的数据流 NSLog(@"received data: %@",[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]); } return YES;} 整个socket的创建及调用部分:self.udpSocket=[[AsyncUdpSocket alloc] initWithDelegate:self];[udpSocket bindToPort:8001 error:&error]; [udpSocket enableBroadcast:YES error:&error]; [udpSocket joinMulticastGroup:@"224.0.0.1" error:&error];[udpSocket receiveWithTimeout:-1 tag:1];接收部分:-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{ NSLog(@"received data: %@",[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]); NSLog(@"%@",host); if(tag==1) { if([[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease] compare:@"ihd"]) { hostIP=[[NSString alloc] initWithString:host]; //[udpSocket close]; } } if(tag==sn) { //先测试是否能够获取到指定的数据流 NSLog(@"received data: %@",[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]); } return YES;}请帮忙看看 把 [udpSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];添加到self.udpSocket=[[AsyncUdpSocket alloc] initWithDelegate:self];后面试试 开发sharepoint手机应用,出现的返回指定数据的问题。 iOS关闭多个模态视图 为什么我的Xcode安装后运行要加载着dmg安装文件才可以? 鸡蛋形滚动实现? initWithWindowNibName 的使用疑问 Xcode环境下,如果查看utf8的字符串值 iOS7中的计算器输入时候字体自动缩小的功能是如何实现的? IOS开发如何在服务器上保存用户信息? UIImagePickerController 不走Delegate方法 有一个比较棘手的需求希望大家能帮我想个思路 这里有哪位大哥了解 Unity3D开发吗? 咨询下 iPhone 和 Java Socket通讯问题。
-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
NSLog(@"received data: %@",[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
NSLog(@"%@",host);
if(tag==1)
{
if([[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease] compare:@"ihd"])
{
hostIP=[[NSString alloc] initWithString:host];
//[udpSocket close];
}
}
if(tag==sn)
{
//先测试是否能够获取到指定的数据流
NSLog(@"received data: %@",[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
}
return YES;
}
self.udpSocket=[[AsyncUdpSocket alloc] initWithDelegate:self];
[udpSocket bindToPort:8001 error:&error];
[udpSocket enableBroadcast:YES error:&error];
[udpSocket joinMulticastGroup:@"224.0.0.1" error:&error];
[udpSocket receiveWithTimeout:-1 tag:1];
接收部分:
-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
NSLog(@"received data: %@",[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
NSLog(@"%@",host);
if(tag==1)
{
if([[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease] compare:@"ihd"])
{
hostIP=[[NSString alloc] initWithString:host];
//[udpSocket close];
}
}
if(tag==sn)
{
//先测试是否能够获取到指定的数据流
NSLog(@"received data: %@",[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
}
return YES;
}请帮忙看看