1
iOS6.1에서 다음 코드는 똑같은 작업을 수행해야하지만 addr.sin_len = sizeof(adde)
을 쓰는 것을 잊어 버리면 첫 번째 블록이 실패했습니다. 원래 오류 :CFStreamCreatePairWithSocketToHost가 성공했을 때 CFStreamCreatePairWithSocketToCFHost가 실패하는 이유
GOT EVENT FROM INPUT Event: 8
ERR: Error Domain=NSPOSIXErrorDomain Code=12 "The operation couldn’t be completed. Cannot allocate memory"
구조체 크기를 설정 누락 라인에 첨가 한 후, 첫 번째 블록은 상기 제 같이했다. 다른 개발자가이 오류 메시지와 여행을이 게시물에서 볼 것입니다.
코드 :
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
#if 1 // LONG WAY
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr); // ORIGINALLY WAS MISSING
addr.sin_family = AF_INET;
addr.sin_port = htons(5566);
int ret = inet_pton(AF_INET, "192.168.1.2", &(addr.sin_addr.s_addr)); // IPv4
assert(ret == 1);
NSLog(@"CONNECT");
CFDataRef address = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)&addr, sizeof(addr));
assert(address);
CFHostRef macMini = CFHostCreateWithAddress(kCFAllocatorDefault, address);
CFRelease(address);
assert(macMini);
// (tried, makes not difference) CFHostScheduleWithRunLoop (macMini, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, macMini, 5566, &readStream, &writeStream);
CFRelease(macMini);
#else // SHORT WAY
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, CFSTR("192.168.1.2"), 5566, &readStream, &writeStream);
#endif
assert(readStream);
assert(writeStream);
iStream = CFBridgingRelease(readStream);
oStream = CFBridgingRelease(writeStream);
[iStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[iStream open];
NSLog(@"ISTREAM %@ status=%d", iStream, [iStream streamStatus]);
NSLog(@"ERR: %@", [iStream streamError]);
[oStream setDelegate:self];
[oStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[oStream open];
NSLog(@"OSTREAM %@ status=%d", oStream, [oStream streamStatus]);
NSLog(@"ERR: %@", [oStream streamError]);