Hi,
I (apparently) manage to make a ftp connection, but fail to read anything from it, and with good cause: I don't reach the reading until the connection has timed out.
Here's my code:
header:
NSInputStream *iStream;
NSOutputStream *oStream;
implementation:
NSHost *host = [NSHost hostWithAddress:@"127.0.0.1"];
[iStream open]; // SOLUTION changes
[NSStream getStreamsToHost:host port:3333 inputStream:&iStream outputStream:&oStream];
NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:1];
[settings setObject:(NSString *)NSStreamSocketSecurityLevelTLSv1 forKey:(NSString *)kCFStreamSSLLevel];
[settings setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCFStreamSSLAllowsAnyRoot];
[settings setObject:@"127.0.0.1" forKey:(NSString *)kCFStreamSSLPeerName];
/*[iStream retain];
[iStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
CFReadStreamSetProperty((CFReadStreamRef)iStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
[iStream setProperty:NSStreamSocketSecurityLevelTLSv1 forKey:NSStreamSocketSecurityLevelKey];*/
//[iStream open]; SOLUTION changes: moved up
For eventhandling I use this, based on http://www.cocoadev.com/index.pl?NSStream:
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
if (aStream == iStream) {
[self handleInputStreamEvent:eventCode];
} else if (aStream == oStream) {
[self handleOutputStreamEvent:eventCode];
}
}
- (void)handleInputStreamEvent:(NSStreamEvent)eventCode
{
switch (eventCode) {
// SOLUTION changes: new inputstream handler
case NSStreamEventHasBytesAvailable:
if(!_data) {
_data = [[NSMutableData data] retain];
}
uint8_t buf[1024];
unsigned int len = 0;
len = [(NSInputStream *)iStream read:buf maxLength:1024];
if(len) {
[_data appendBytes:(const void *)buf length:len];
[bytesRead setIntValue:[bytesRead intValue]+len];
}
[self readBytes];
break;
case NSStreamEventOpenCompleted:
NSLog(@"NSStreamEventOpenCompleted");
break;
case NSStreamEventEndEncountered:
NSLog(@"NSStreamEventEndEncountered");
break;
case NSStreamEventErrorOccurred:
NSLog(@"An error occurred on the input stream.");
break;
}
}
I've left out oStream setup from this post to keep it minimal.
I have not sent a request to the FTP to switch to ssl yet.
Any help is greatly appreciated as I find Xcode quite horrible for debugging (no exception or error msg on failed steps what so ever).
- Chuck