NSStream关闭和打开错误 [英] NSStream closing and opening error

查看:124
本文介绍了NSStream关闭和打开错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与NSStream一起通过网络连接发送和接收单个字符.
我使用CFStreamCreatePairWithSocketToCFHost(...)实例化了用于读取和写入的流.

发送和接收的基本工作机制运行正常.

现在,我只想在需要发送或接收内容(通常每2分钟发送一次)时创建NSStream.第一次创建流时,发送数据包,关闭它,一切正常.接下来,我尝试重新创建NSStream(创建一个新的套接字并在其上打开流),打开功能不起作用.由于我正在使用netcat对其进行测试,因此我发现使用[NSStream close]方法关闭流时,服务器上的连接未关闭.如何强制关闭插槽?还是有更好的方法来做我想做的事情?

解决方案

使用CFWriteStreamSetPropertyCFReadStreamSetPropertykCFStreamPropertyShouldCloseNativeSocket设置为kCFBooleanTrue,以便在关闭流时关闭基础套接字. >

I'm working with NSStream to send and receive single characters over a network connection.
I instantiate the streams both for reading and for writing using CFStreamCreatePairWithSocketToCFHost(...).

The basic working mechanism to send and receive is working fine.

Now I want to create the NSStream only when I need to send or receive something (usually one time every 2 minutes). The first time I create the streams, send the packet, close it and everything work well. Next I try to recreate the NSStream (create a new socket and open the stream on it), the opening function don't work. Since I'm using netcat to test it, I see that when I close the stream using [NSStream close] method, the connection on the server is not closing. How can I force to close the socket? Or there's a better way to do what I'm trying to do?

解决方案

Use CFWriteStreamSetProperty and CFReadStreamSetProperty to set kCFStreamPropertyShouldCloseNativeSocket to kCFBooleanTrue in order that the underlying socket is closed when the stream is closed.

这篇关于NSStream关闭和打开错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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