Cocoa NSStream TCP 连接到 FTP

发布于 2024-08-27 21:53:57 字数 1315 浏览 7 评论 0原文

我是 Cocoa 新手,但不是编程新手。 最近,我决定为 Mac 编写一个 FTP 客户端,因此我首先用我最熟悉的语言(在 Windows 上)编写它,然后当我完成 FTP 通信的工作后,转向 Cocoa。

我的问题(显然)有点争议:如何建立到(ftp 服务器)的读/写连接?

到目前为止我所拥有的(显然不起作用):

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];

部分基于 http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/Streams/Articles/NetworkStreams.html 现在,我为什么选择 NSStream?因为虽然这个问题只是关于如何连接到 FTP 流,但我的整个项目还将包括 SSL,并且据我能够在此处和 google 上搜索,NSStream 能够“切换”到 SSL 连接。

我无法看到正在建立的连接(我通常能够做到),但我也听说过在流打开之前必须写入流的信息?

非常感谢任何指点,如果我的问题很烦人,我很抱歉 - 我是 Cocoa 的新手:)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面犯桃花 2024-09-03 21:53:57

所以当我说我是 Cocoa 新手时,我是认真的。事实证明上面的代码毕竟有效,我只是看不到它,因为我希望在这一行建立连接:
[NSStream getStreamsToHost:主机端口:3333 inputStream:&iStream outputStream:&oStream];
但直到我出于调试目的在底部添加了 NSRunAlert 之后才制作它。

我不确定我是否比困惑更尴尬,呵呵。

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.

紫轩蝶泪 2024-09-03 21:53:57

您需要实现handleEvent委托方法并观察输入和输出流对象发生了什么...看看文档,那里有解释!

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文