我使用AsyncUdpSocket
在iphone中广播UDP消息,Android手机无法接收任何消息。发送以下UDP代码:
- (void)viewDidLoad {
[super viewDidLoad];
[self setupClientUdp];
}
- (void)setupClientUdp
{
_clientUdp = [[AsyncUdpSocket alloc]initWithDelegate:self];
NSError *err = nil;
[_clientUdp enableBroadcast:YES error:&err];
[self check:err];
[_clientUdp bindToPort:9997 error:&err];
[self check:err];
}
- (void)check:(NSError *)err
{
NSLog(@"err=%@",err.domain);
}
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
return YES;
}
- (void)searchServer
{
NSString *str = @"who is there?";
NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
[_clientUdp sendData:data toHost:@"255.255.255.255" port:9998 withTimeout:-1 tag:0];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self searchServer];
}
我写了一个iOS项目来接收iphone上的Udp消息,它可以接收UDP。UDP
协议应该是平台无关的吗?或AsyncUdpSocket
做不同的事情?