使用NSOutputStream通过套接字发送数据的正确方法 [英] Correct way to send data through a socket with NSOutputStream

查看:176
本文介绍了使用NSOutputStream通过套接字发送数据的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在iOS上套接字编程,我很难确定 NSStreamEventHasSpaceAvailable 事件的使用 NSOutputStreams

I am just getting started with socket programming on iOS and I am struggling to determine the use of the NSStreamEventHasSpaceAvailable event for NSOutputStreams.

一方面,官方文档(清单2)显示,在 -stream:handleEvent:委托方法中,数据应该写入输出缓冲区, -write:maxLength:消息,每当收到 NSStreamEventHasSpaceAvailable 事件时,从缓冲区连续传递数据。

On the one hand, Apple's official documentation (Listing 2) shows that in the -stream:handleEvent: delegate method, data should be written to the output buffer with -write:maxLength: message, passing data continually from a buffer, whenever the NSStreamEventHasSpaceAvailable event is received.

另一方面,本教程从Ray Wenderlich 这个iOS GitHub上的TCP套接字示例忽略了 NSStreamEventHasSpaceAvailable 事件,只需执行 -write:maxLength:

On the other hand, this tutorial from Ray Wenderlich and this iOS TCP socket example on GitHub ignore the NSStreamEventHasSpaceAvailable event altogether, and just go ahead and -write:maxLength: to the buffer whenever they need to (even ignoring -hasSpaceAvailable).

第三,还有 =http://www.ios-developer.net/iphone-ipad-programmer/development/tcpip/tcp-client>此示例代码看起来像两者 .. 。

Thirdly, there is this example code which appears to do both...

因此,我的问题是,处理将数据写入 NSOutputStream 的正确方法是什么是连接到套接字?如果可以(显然)被忽略, NSStreamEventHasSpaceAvailable 事件代码的用途是什么?在我看来,有非常幸运的UB发生(在例子2和3),或有几种方式通过基于套接字的 NSOutputStream ...发送数据...

My question is therefore, what is the correct way(s) to handle writing data to an NSOutputStream that is attached to a socket? And of what use is the NSStreamEventHasSpaceAvailable event code if it can (apparently) be ignored? It seems to me that there is either very fortunate UB happening (in examples 2 and 3), or there are several ways of sending data through a socket-based NSOutputStream...

推荐答案

您可以随时写入流,但对于网络流, -write:maxLength :只有在至少有一个字节被写入套接字写入缓冲区之后才会返回。因此,如果套接字写缓冲区已满(例如,因为连接的另一端没有足够快地读取数据),
这将会阻塞当前线程。如果你从主线程写,这将阻止
的用户界面。

You can write to a stream at any time, but for network streams, -write:maxLength: returns only until at least one byte has been written to the socket write buffer. Therefore, if the socket write buffer is full (e.g. because the other end of the connection does not read the data fast enough), this will block the current thread. If you write from the main thread, this will block the user interface.

NSStreamEventHasSpaceAvailable 当您可以写入流
而不阻塞时,会发出信号。写入只响应该事件,避免当前线程
和可能的用户界面被阻塞。

The NSStreamEventHasSpaceAvailable event is signalled when you can write to the stream without blocking. Writing only in response to that event avoids that the current thread and possibly the user interface is blocked.

或者,您可以从网络流单独的作者线程。

Alternatively, you can write to a network stream from a separate "writer thread".

这篇关于使用NSOutputStream通过套接字发送数据的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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