views:

500

answers:

2

Hi,

I'm new to Cocoa, but not to programming. Recently I decided I wanted to write a FTP client for Mac, and so I first made it in the language I'm most comfortable in (on Windows), and then moved on to Cocoa when I had the workings of FTP communications down.

My question is (apparently) a bit controversial: How do I establish a read/writeable connection to (a ftp server)?

What I have so far (non working obviously):

NSInputStream *iStream;
NSOutputStream *oStream;
NSHost *host = [NSHost hostWithAddress:@"127.0.0.1"];
[NSStream getStreamsToHost:host port:3333 inputStream:&iStream outputStream:&oStream];
// ftp port: 3333
[iStream retain];
[oStream retain];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
             forMode:NSDefaultRunLoopMode];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
             forMode:NSDefaultRunLoopMode];
[iStream setDelegate:self];
[oStream setDelegate:self]; // which is not implemented apparently
[iStream open];
[oStream open];
// .... [iStream write: (const uint8_t *)buf maxLength:8];

Which is partially based on http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/Streams/Articles/NetworkStreams.html Now, why have I chosen NSStream? Because while this question is merely about how to connect to a FTP stream, my whole project will also include SSL and as far as I've been able to search here and on google, NSStream is capable of "switching" to a SSL connection.

I've not been able to see the connection being made (which I'm usually able to do), but I also heard something about having to write to the stream before the stream will open?

Any pointers are greatly appreciated, and sorry if my question is annoying - I'm new to Cocoa :)

+1  A: 

So when I said I was new to Cocoa I meant every word of it. Turns out the code above worked after all, I just wasn't able to see it because I expected the connection to be made at this line: [NSStream getStreamsToHost:host port:3333 inputStream:&iStream outputStream:&oStream]; but it wasn't made until after a NSRunAlert I added at the bottom for debug purpose.

I'm not sure wether I'm more embaressed than I am confused hehe.

James
A: 

You need to implement handleEvent delegate method and watch what is happening to your Input and Output stream objects... Take a look at the docs, it's explained there!

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
Primoz Rome