[英]End/Stop a NSFileHandle socket server
I created a server using a NSFileHandle and it works just fine the problem is how to end it. 我使用NSFileHandle创建了一个服务器,它工作得很好,问题是如何结束它。 (close it) I cannot dealloc it because I'm using ARC and I really have to find a way to end it. (关闭它)我无法取消分配它,因为我正在使用ARC,我确实必须找到一种方法来结束它。 Here is how I started the server: 这是我启动服务器的方式:
socketPort = [[NSSocketPort alloc] initWithTCPPort:portNumber];
int fd = [socketPort socket];
fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd
closeOnDealloc:YES];
Thanks! 谢谢!
What about [NSFileHandle closeFile]
: 那[NSFileHandle closeFile]
?
[fileHandle closeFile];
fileHandle = nil; // ARC equiv of release
(I would imagine the [NSFileHandle dealloc]
method calls closeFile
anyway, but calling it directly is more obvious of your intent and is more defensive). (我想[NSFileHandle dealloc]
方法仍然会调用closeFile
,但是直接调用它对您的意图更明显,而且更具防御性)。
If you want to stop socketPort
from listening, then this will probably work: 如果您想停止socketPort
的监听,那么可能会起作用:
socketPort = nil;
WARNING: According to Apple you aren't supposed to use NSSocketPort
and NSFileHandler
for general networking. 警告: 根据Apple的说法,您不应该将NSSocketPort
和NSFileHandler
用于常规联网。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.