Google コードの質問ミラー: https://groups.google.com/forum/#!topic/cocoaasyncsocket/grhjZSMLr3U
応答を読んでいる私のコードは次のとおりです。
- (void)init {
udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
if (![udpSocket bindToPort:UDP_PORT error:&error]) { //not connecting to host
return;
}
if (![udpSocket beginReceiving:&error]) {
return;
}
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock
didReceiveData:(NSData *)data
fromAddress:(NSData *)address
withFilterContext:(id)filterContext
{
NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"got data from broadcast: %@",msg);
NSString *src = [[NSString alloc] initWithData:address encoding:NSUTF8StringEncoding];
NSLog(@"got src from broadcast: %@",src);
}
udp bcast を送信するコードは次のとおりです。
- (void)send {
[udpSocket enableBroadcast:YES error:nil];
[udpSocket sendData:[self pack:@"134.71.146.49"] toHost:UDP_BCAST_ADDR port:UDP_PORT withTimeout:-1 tag:UDP_BROADCAST_SEND];
}
port と bcast addr は、それぞれ 55555 と 255.255.255.255 です。
コンソール出力:
got data from broadcast: 134.71.146.49
got src from broadcast: (null)
got data from broadcast: 134.71.146.49
got src from broadcast: (null)
16 進数:
data: <3133342e 37312e31 34362e34 39>
address: <1002d903 864793dd 00000000 00000000>
why is the source address here being null? Or why is it malformed?