0
어떻게 TCP 소켓을 통해 배열을 보내고받을 수 있는지 알아 내려고합니다. 나는 객관적인 C에서 약간의 초보자이지만 문자열을 보내고받을 수있었습니다. 이제 배열을 처리하려고합니다.Obj-C는 iphone 용 tcp 소켓을 통해 배열을 보내고받습니다.
(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");
}
}
그게 내가 가진 것. 어디에 넣었습니까? // 배열을 삽입하려고합니다.
도움을 주시면 미리 감사드립니다.
멋진 덕분에 문자열에서 약 200 개의 값을 송신자와 수신자 측에서 쉽게 전송할 수 있지만 트래픽을 억제하기위한 최선의 방법입니까? – NickREd
가장 좋은 방법은 보내는 데이터 구조를 최적화하는 것입니다. 필요한 것을 보내면됩니다. 예를 들어 반바지 만 필요하면 정수를 보내지 말고 ... "뚱뚱한"리시버가 필요한지 아닌지 결정해야합니다. 예를 들어 평균을해야하는 경우 모든 값 (뚱뚱보 수신기), 또는 보낸 사람에 계산하고 결과를 보낼 수 있습니다. – fbernardo