我将从iPhone发送UDP广播,然后在打开此类端口的所有设备上监听具有超时时间的UDP响应。来自同一子网的我的自定义设备会回答吗? (如果我的是192.168.1.100,自定义设备的IP是192.168.1.201) 什么用“SmallSockets”或“cocoaAsyncSocket”? 用什么功能来听取回应? 感谢名单!
答案 0 :(得分:3)
我决定使用cocoaAsyncSocket。 要广播,您可以使用:
[udpSocket sendData:datatosend toHost:@"192.168.1.113" port:port withTimeout:-1 tag:0];
接收:
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext
{
NSString *msg = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSString *host = nil;
uint16_t port = 0;
[GCDAsyncUdpSocket getHost:&host port:&port fromAddress:address];
if (msg)
{
NSLog(@"Message = %@, Adress = %@ %i",msg,host,port);
}
else
{
NSLog(@"Error converting received data into UTF-8 String");
}
}