Socket Timeout - Need Help
I'm using this implementation to connect to a server socket, when i tried to connect to an inexistent socket server it enters in a endless loop, how can i set a timeout?
+ (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");
开发者_如何学Python
}
Thanks in advance!
If you want a timeout, schedule a timer. If the timer fires before the NSStreamEventOpenCompleted
arrives, then you've timed out. Respond as appropriate (shut down the streams, etc.).
精彩评论