简体   繁体   中英

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. (close it) I cannot dealloc it because I'm using ARC and I really have to find a way to end it. 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] :

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

If you want to stop socketPort from listening, then this will probably work:

socketPort = nil;

WARNING: According to Apple you aren't supposed to use NSSocketPort and NSFileHandler for general networking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM