NSStream freezes

I have NSInputStream and NSOutputStream in an iphone application that are connected to a server. I can read and write data to sockets without any problems. The problem is that I have a disconnect button, and when I try to call close to any of the streams, this freezes the application. I guess I'm trying to call close at the wrong time, but I'm not sure what time it is.

+3
source share
3 answers

Most likely, you did not save your input and output streams after receiving them :) I had exactly the same problem, but it took me a lot of time to find a solution :)

+2
source

, , . , :

...
CFReadStreamRef readStreamRef;
CFWriteStreamRef writeStreamRef;

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)host, port, &readStreamRef, &writeStreamRef);

if (readStreamRef && writeStreamRef)
{
    CFReadStreamSetProperty(readStreamRef, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
    CFWriteStreamSetProperty(writeStreamRef, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);

    inputStream = (NSInputStream*)readStreamRef;
    outputStream = (NSOutputStream*)writeStreamRef;
    ...
+1

Saving NSOutputStream solves my problem, hope this helps others. Sample codes are written using ARC, so they no longer need to save, but if you do not use ARC, you must save.

NSOutputStream *outputStream = (NSOutputStream *)CFBridgingRelease(writeStream);
**[outputStream retain];**
[outputStream setDelegate:self];
[outputStream scheduleInRunLoop:[NSRunLoop mainRunLoop]
                                forMode:NSDefaultRunLoopMode];
[outputStream open];
0
source

Source: https://habr.com/ru/post/1756138/


All Articles