1

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?

alpine
  • 927
  • 6
  • 17
  • Since you are using a global broadcast address 255.255.255.255, you need to enable `socket.setBroadcast(true)` http://stackoverflow.com/questions/6579350/sending-packets-to-255-255-255-255-by-java-datagramsocket-fails ;othewise, use a simple multicast IP address group – ecle Nov 12 '15 at 02:06
  • I have `[_clientUdp enableBroadcast:YES error:&err]` in code – alpine Nov 12 '15 at 03:02
  • I mean on the Android client side...otherwise use normal multicast/unicast IP address to test first instead of broadcast IP address – ecle Nov 12 '15 at 03:23
  • You need to isolate the problem: Is the UDP actually being sent? Or is the problem at the receiving end? Wireshark on a proper computer on the same network will answer that question. – Chris Nov 12 '15 at 07:44

0 Answers0