소켓 통신 read:maxLength: 문제
조회수 1108회
Java로 서버를 작성하고 iPhone 앱 (Objective C)을 사용하여 통신하려고합니다. 연결이 완료되고 서버에 데이터를 쓸 수는 있지만 서버에서 데이터를 다시 읽을 수는 없습니다. 읽을 데이터가 있다는 알림이 표시되지만 비어 있습니다. 제가 생각하기에는 read:maxLength: 이부분에서 문제가 생긴것같습니다.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^(void) {
NSLog(@"staying alive!");
[[DABMultitaskingController sharedInstance] startTask];
}];
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"10.0.1.189", 4444, &readStream, &writeStream);
NSInputStream *inputStream = (NSInputStream *)readStream;
NSOutputStream *outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
_dataToSend = [[NSData dataWithBytes:"This is a test" length:15] retain];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES; }
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
switch (streamEvent) {
case NSStreamEventHasBytesAvailable: {
NSLog(@"can read: %@", theStream);
uint8_t *buffer;
NSUInteger length;
[(NSInputStream *)theStream getBuffer:&buffer length:&length]; //여기서 문제
NSData *data = [NSData dataWithBytes:buffer length:length]; // 여기서 문제
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"bytes: %@", data);
NSLog(@"string: %@", string);
break;
}
case NSStreamEventHasSpaceAvailable: {
NSLog(@"can write: %@", theStream);
if (_dataToSend != nil) {
NSLog(@"write: %@", _dataToSend);
[(NSOutputStream *)theStream write:[_dataToSend bytes] maxLength:[_dataToSend length]];
[_dataToSend release];
_dataToSend = nil;
}
break;
}
case NSStreamEventOpenCompleted: {
NSLog(@"open complete: %@", theStream);
break;
}
case NSStreamEventErrorOccurred: {
NSLog(@"error occurred: %@", theStream);
break;
}
case NSStreamEventEndEncountered: {
NSLog(@"end encountered: %@", theStream);
break;
}
default:
break;
} }
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력