Cocoa NSOutputStream 发送到连接 [英] Cocoa NSOutputStream send to a connection

查看:39
本文介绍了Cocoa NSOutputStream 发送到连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Cocoa 的新手,但设法建立并运行了一个连接(到 FTP),并且我为 NSInputStream iStream 设置了一个事件处理程序来提醒每个响应(这也有效).

I am new to Cocoa, but managed to get a connection (to a FTP) up and running, and I've set up an eventhandler for the NSInputStream iStream to alert every response (which also works).

我设法得到的只是问候消息和连接超时 60 秒,关闭控制连接.

What I manage to get is simply the hello message and a connection timeout 60 sec, closing control connection.

我想我的问题是没有关闭->打开刷新输出流的非终止方式是什么?

I guess my question is "without closing->opening what would be an non-terminating way of flushing the outputStream?

搜索stackoverflow,发现很多NSOutputStream写入问题(如如何使用NSOutputStream的写消息?)和我的谷歌点击中有很多混乱,我想我会尝试问我自己的问题:

After searching stackoverflow and finding a lot of NSOutputStream write problems (e.g. How to use NSOutputStream's write message?) and a lot of confusion in my google hits, I figured I'd try to ask my own question:

我已尝试阅读关于 OutputStream 的 developer.apple.com 文档,但我似乎几乎不可能通过 NSOutputStream oStream 将一些数据(在本例中只是一个字符串)发送到连接".

I've tried reading the developer.apple.com doc on OutputStream, but it seems almost impossible for me to send some data (in this case just a string) to the "connection" via the NSOutputStream oStream.

- (IBAction) send_something: sender
{
const char *send_command_char = [@"USER foo" UTF8String];
send_command_buffer = [NSMutableData dataWithBytes:send_command_char length:strlen(send_command_char) + 1];
uint8_t *readBytes = (uint8_t *)[send_command_buffer mutableBytes];
NSInteger byteIndex = 0;
readBytes += byteIndex;
int data_len = [send_command_buffer length];
unsigned int len = ((data_len - byteIndex >= 1024) ?
                    1024 : (data_len-byteIndex));
uint8_t buf[len];
(void)memcpy(buf, readBytes, len);
len = [oStream write:(const uint8_t *)buf maxLength:len];
byteIndex += len;
}

以上似乎不会导致任何可用的事件.在 NSStreamEventHasSpaceAvailable 下键入它有时会给出响应,如果我通过不断创建新的连接实例向 ftp 发送垃圾邮件并在 oStream 有可用空间时继续发送一些命令.换句话说,没有什么正确"的,所以我仍然不清楚如何正确地向连接发送命令.每次我想写入 oStream(以及 ftp)时,我是否应该打开 -> 写入 -> 关闭,然后我可以期待回复(iStream 上的 hasBytesAvailable 事件)吗?看起来不像.

the above seems not to result in any useable events. typing it under NSStreamEventHasSpaceAvailable sometimes give a response if I spam the ftp by keep creating new connection instances and keep sending some command whenever oStream has free space. In other words, nothing "right" and so I'm still unclear how to properly send a command to the connection. Should I open -> write -> close every time i want to write to oStream (and thus to the ftp) and can I then expect a reply (hasBytesAvailable event on iStream)? Doesn't look like it no.

由于某种原因,我发现很难找到任何关于这个问题的清晰教程.好像和我一样位置的不止几个:不清楚如何使用oStream write?

For some reason I find it very difficult to find any clear tutorials on this matter. Seems like there are more than a few in the same position as me: unclear how to use oStream write?

请!非常感谢任何可以帮助解决此问题的一点点!

Please! Any little bit that can help clear this up is greatly appreciated!

如果需要,我可以编写其余代码.

If needed I can write the rest of the code.

  • 查克

推荐答案

好的,所以 10 小时 28 次查看,没有答案/评论,但没关系,因为我刚刚在一个非常友好的 irssi 编码器的帮助下解决了这个问题(无意舔屁股;)).

Okay, so 10 hours 28 views and no answers/comments, but that's OK, because I just solved it with some good help from a very very friendly irssi coder (no butt licking intended ;)).

他建议我尝试 std::endl'e 它(换行符 + 刷新),所以我尝试简单地添加换行符( ,0x0A)并且效果很好!

He proposed that I tried to std::endl'e it (newline + flush), and so I tried simply adding the newline char ( , 0x0A) and it worked perfectly!

这篇关于Cocoa NSOutputStream 发送到连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆