我想弄清楚如何以及是否可以通过TCP套接字发送和接收数组。我在目标C上有点新手,但我能够发送和接收字符串。我现在只想让它做阵列。
(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
NSLog(@"stream event %i", streamEvent);
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
break;
case NSStreamEventHasBytesAvailable:
if (theStream == inputStream) {
uint8_t buffer[1024];
int len;
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output) {
chSent = [output substringWithRange: NSMakeRange (0, 6)];
dimensionString = [output substringWithRange: NSMakeRange (7, 3)];
colorString = [output substringWithRange: NSMakeRange (7, 3)];
if ([chSent isEqualToString:@"dimen:"])
{
dimensionInt = [dimensionString intValue];
}
if ([chSent isEqualToString:@"color:"]) {
// insert array named color in here some how
}
}
}
}
}
break;
case NSStreamEventErrorOccurred:
NSLog(@"Can not connect to the host!");
break;
case NSStreamEventEndEncountered:
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
// [theStream release];
theStream = nil;
break;
default:
NSLog(@"Unknown event");
}
}
这就是我所拥有的。 我放在哪里//插入数组是我试图把它放在哪里
如果您能提供帮助,请提前致谢。
答案 0 :(得分:1)
您可以将数组数据(例如整数)作为字符串除以某个分隔符“color:1; 2; 3; 4;”然后只是分开字符串。如果您需要传递更复杂的数据结构,您可以编码,发送,解码。看看NSKeyedArchiver类。