0

大家好,我在学校做一个项目,我遇到了一个问题,我可以将 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;
}
4

1 回答 1

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;
于 2013-05-16T15:38:20.690 回答