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

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

问题描述

我是Cocoa新手,但是设法连接(到FTP)并运行,我已经为NSInputStream iStream设置了一个事件处理程序来提醒每个响应。

我管理得到的只是hello消息和连接超时60秒,关闭控制连接。



编辑:I猜测我的问题是没有关闭 - >打开什么将是一个非终止的方式刷新outputStream?



搜索stackoverflow和发现很多NSOutputStream写问题例如如何使用NSOutputStream的写信息?)和我的很多困惑google命中,我想我会尝试问我自己的问题:



我试过在OutputStream上阅读developer.apple.com doc,但似乎几乎不可能为我通过NSOutputStream oStream发送一些数据(在这种情况下只是一个字符串)到连接。

   - IBAction)send_something:sender 
{
const char * send_command_char = [@USER fooUTF8String];
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),然后我可以期望一个回复(hasBytesAvailable事件在iStream)?
编辑:看起来不像没有。



由于某种原因,我发现很难找到任何明确的教程。似乎有几个在同一个位置与我:不清楚如何使用oStream写?



请!非常感谢任何可以帮助清除这一点的一点点。



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




  • Chuck


解决方案

28查看,没有答案/评论,但是没关系,因为我刚刚解决它与一个非常非常友好的irssi编码器(没有屁股舔意图;)一些很好的帮助。)



他建议我试图std :: endl'e it(newline + flush),所以我试着简单地添加换行符(\\\
,0x0A),它工作完美!


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

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

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

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:

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

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)? EDIT: Doesn't look like it no.

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.

  • Chuck

解决方案

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 ;)).

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

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

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