嗨,大家好我在学校的一个项目工作,我遇到了一个问题,我能够将UDP数据包发送到不同的地址,但我收不到发送到我的iPhone的UDP数据包,收到UDP数据包后,我想要数据出现在标签上,我做错了什么但我不知道是什么.....我的代码:
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
NSLog(@"Incoming data");
[sock receiveWithTimeout:1 tag:0];
NSString *receiveddata = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
// NSString *receiveddata = data ;
self.testudp.text = receiveddata;
return YES;
}
答案 0 :(得分:0)
这就是我所做的改变:
AsyncUdpSocket *udpsocket;
udpsocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
[udpsocket enableBroadcast:YES error:nil];
[udpsocket bindToPort:1234 error:nil];
[udpsocket receiveWithTimeout:-1 tag:0];
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{
NSLog(@"received data");
NSString *receiveddata = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
_testudp.text = receiveddata;