关于在 iPhone 中通过点对点网络发送文本的问题
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过将 NSData* 类型转换为 char* 并使用 -stringWithCString:encoding: 从 CString 获取 NSString 可以解决该问题。
The problem is solved by type cast NSData* to char* and use
-stringWithCString:encoding:
to get NSString from CString.