2011-03-28 4 views
3

이 구현을 사용하여 서버 소켓에 연결합니다. 무한 루프로 입력하는 존재하지 않는 소켓 서버에 연결하려고 시도하면 어떻게 타임 아웃을 설정할 수 있습니까?소켓 타임 아웃 - 도움이 필요합니다

+ (void)getStreamsToHostNamed:(NSString *)hostName 
         port:(NSInteger)port 
        inputStream:(NSInputStream **)inputStreamPtr 
       outputStream:(NSOutputStream **)outputStreamPtr 

{ 
    CFReadStreamRef  readStream; 
    CFWriteStreamRef writeStream; 

    assert(hostName != nil); 
    assert((port > 0) && (port < 65536)); 
    assert((inputStreamPtr != NULL) || (outputStreamPtr != NULL)); 

    readStream = NULL; 
    writeStream = NULL; 

    CFStreamCreatePairWithSocketToHost(
             NULL, 
             (CFStringRef) hostName, 
             port, 
             ((inputStreamPtr != nil) ? &readStream : NULL), 
             ((outputStreamPtr != nil) ? &writeStream : NULL) 
             ); 

    if (inputStreamPtr != NULL) { 
     *inputStreamPtr = [NSMakeCollectable(readStream) autorelease]; 
    } 
    if (outputStreamPtr != NULL) { 
     *outputStreamPtr = [NSMakeCollectable(writeStream) autorelease]; 
    } 
} 

@end 

@implementation sockets 

@synthesize iStream; 
@synthesize oStream; 
@synthesize model; 

- (void) connect: (NSString*) IPAdress and:(NSInteger) porto{ 

    NSString *temporary = [NSString stringWithFormat: @"%@", IPAdress]; 
    [NSStream getStreamsToHostNamed:temporary port:porto 
         inputStream:&iStream 
         outputStream:&oStream]; 

    [iStream retain]; 
    [oStream retain]; 
    [iStream setDelegate:self]; 
    [oStream setDelegate:self]; 
    [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [iStream open]; 
    [oStream open]; 
    NSLog (@"Conectado"); 


} 

미리 감사드립니다.

답변

4

시간 초과가 필요하면 타이머를 예약하십시오. NSStreamEventOpenCompleted이 도착하기 전에 타이머가 작동하면 시간이 초과되었습니다. 적절하게 응답하십시오 (하천 등을 차단하십시오).