I have used AsyncUdpSocket
to broadcast UDP message in iphone,Android phone can't receive any message. Send UDP code below:
- (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];
}
I write a iOS project to receive Udp message on iphone, it can received UDP。 UDP
protocol should be platform independent? or AsyncUdpSocket
do sth different?