关于在 iPhone 中通过点对点网络发送文本的问题

发布于 2024-10-21 18:51:00 字数 1452 浏览 5 评论 0原文

我正在尝试通过 iPhone 中的点对点网络发送一些文本。我对 GKTank 示例进行了一些修改,使其发送字符串而不是自定义的结构数据。以下是我编写的代码。我的问题是,我可以将@“hello”发送到另一个ios设备,它可以接收数据(长度为5),但是它接收到的NSData无法转换为NSString,因此接收到的数据无法显示为字符串。关于代码有什么问题有什么想法吗?

- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context 
{
    NSString *recvStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    textField.text = recvStr;
    [recvStr release];
}

- (void)sendNetworkPacket:(GKSession *)session packetID:(int)packetID withData:(void *)data ofLength:(int)length reliable:(BOOL)howtosend
{
    NSData *packet = [NSData dataWithBytes:data length:length];
    NSError *error = nil;
    if(howtosend == YES) { 
        [session sendData:packet toPeers:[NSArray arrayWithObject:gamePeerId] withDataMode:GKSendDataReliable error:&error];
    } else {
        [session sendData:packet toPeers:[NSArray arrayWithObject:gamePeerId] withDataMode:GKSendDataUnreliable error:&error];
    }
    if (!error) {
        NSLog(@"Did send data");
    } else {
        NSLog(@"Send data failed: %@", [error localizedDescription]);
    }

}

- (IBAction)sendText:(id)sender
{
    if (self.gameSession && textField.text.length) {
        NSData *data = [textField.text dataUsingEncoding:NSUTF8StringEncoding];
        [self sendNetworkPacket:gameSession packetID:0 withData:data ofLength:[data length] reliable:NO];
    }
}

I'm trying to send some text via Peer to Peer Networking in iPhone. I've modified the GKTank Sample a little to make it send string instead of customized struct data. The following is the code I've written. My problem is that I can send @"hello" to another ios device and it can receive a data (length 5), but the NSData it received cannot be converted to NSString so the received data cannot show as a string. Any ideas about what's wrong with the code?

- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context 
{
    NSString *recvStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    textField.text = recvStr;
    [recvStr release];
}

- (void)sendNetworkPacket:(GKSession *)session packetID:(int)packetID withData:(void *)data ofLength:(int)length reliable:(BOOL)howtosend
{
    NSData *packet = [NSData dataWithBytes:data length:length];
    NSError *error = nil;
    if(howtosend == YES) { 
        [session sendData:packet toPeers:[NSArray arrayWithObject:gamePeerId] withDataMode:GKSendDataReliable error:&error];
    } else {
        [session sendData:packet toPeers:[NSArray arrayWithObject:gamePeerId] withDataMode:GKSendDataUnreliable error:&error];
    }
    if (!error) {
        NSLog(@"Did send data");
    } else {
        NSLog(@"Send data failed: %@", [error localizedDescription]);
    }

}

- (IBAction)sendText:(id)sender
{
    if (self.gameSession && textField.text.length) {
        NSData *data = [textField.text dataUsingEncoding:NSUTF8StringEncoding];
        [self sendNetworkPacket:gameSession packetID:0 withData:data ofLength:[data length] reliable:NO];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

錯遇了你 2024-10-28 18:51:00

通过将 NSData* 类型转换为 char* 并使用 -stringWithCString:encoding: 从 CString 获取 NSString 可以解决该问题。

- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context
{
    unsigned char *incomingPacket = (unsigned char *)[data bytes];
    textField.text = [NSString stringWithCString:(const char *)incomingPacket encoding:NSUTF8StringEncoding];
}

The problem is solved by type cast NSData* to char* and use -stringWithCString:encoding: to get NSString from CString.

- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context
{
    unsigned char *incomingPacket = (unsigned char *)[data bytes];
    textField.text = [NSString stringWithCString:(const char *)incomingPacket encoding:NSUTF8StringEncoding];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文