title: NSStream - The operation couldn’t be completed. Connection refused
body: <p>I am trying to couple 2 devices with NSNetService - <em>Device A</em> publishes the service and <em>Device B</em> can browse it and establish a connection. Yesterday I noticed a pretty nasty issue though. </p>
  
  <p>First time I start the service on <em>Device A</em> and try to open the input/output streams everything works as intended. If I then quit the app on <em>Device A</em> and re-open it, the service gets created ( because netServiceDidPublish gets called ) but when I try opening the streams again I get the following <strong>Error Domain=NSPOSIXErrorDomain Code=61 "The operation couldn’t be completed. Connection refused</strong>. I can then wait a little bit and try opening the app again and streams get opened again as intended.</p>
  
  <p>I thought it had to do with the fact that I am not closing the streams when I am quitting my app. That's why I tried closing the streams in <code>-(void)applicationWillTerminate</code>. The method responsible for closing gets called but the next time I start the app I get the same error messages.</p>
  
  <p>I doodled with CocoaEcho sample code provided by Apple and I can't find any major difference. Or I might be wrong.</p>
  
  <p>Worth mentioning that I created a singleton class for the service publishing. I don't think this should matter but one can never know. Below you can find some of the code I am using for service publishing, stream opening and stream closing. </p>
  
  <p><strong>Open service:</strong></p>
  
  <pre><code>-(void)startServiceWithName:(NSString*)name withType:(NSString*)type {
  
      // init the socket
      if(_socket) {
  
          // socket initialized
          addr = (struct sockaddr*)[[_socket address] bytes];
  
          if (addr-&gt;sa_family==AF_INET) {
              // IPV4
              port = ntohs( ((struct sockaddr_in *)addr)-&gt;sin_port );
          }
  
          if (addr-&gt;sa_family==AF_INET6) {
              // IPV6
              port = ntohs( ((struct sockaddr_in6 *)addr)-&gt;sin6_port );
          }
  
  
      } else {
  
          // socket nil
          _socket = nil;
          NSLog(@"Socket init failed. Can't handle");
  
      }
  
      // init the service
      if(_socket) {
  
          // we have a socket
          _service = [[NSNetService alloc] initWithDomain:@"local."
                                                     type:type
                                                     name:name
                                                     port:port];
  
          if (_service) {
  
              [_service setDelegate:self];
              [_service publish];
  
          }
  
      }
  
  }
  </code></pre>
  
  <p><strong>Service Did Publish:</strong></p>
  
  <pre><code>-(void)netServiceDidPublish:(NSNetService *)sender {
  
      NSLog(@"Service published on port %li", [sender port]);
  
      if(![self openStreams]) {
          NSLog(@"Could not open streams");
      }
  
  }
  </code></pre>
  
  <p><strong>Open streams</strong></p>
  
  <pre><code>-(BOOL)openStreams {
  
      NSInputStream *is;
      NSOutputStream *os;
  
      if ([_service getInputStream:&amp;is outputStream:&amp;os]) {
  
          _inputStream = is;
          _outputStream = os;
  
          [_inputStream setDelegate:self];
          [_outputStream setDelegate:self];
          [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
          [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
          [_inputStream open];
          [_outputStream open];
  
          return YES;
      }
  
      return NO;
  }
  </code></pre>
  
  <p><strong>Close streams</strong></p>
  
  <pre><code>-(void)closeStreams {
      [_inputStream setDelegate:nil];
      [_outputStream setDelegate:nil];
      [_inputStream close];
      [_outputStream close];
  
      [_inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
      [_outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  
      _inputStream = nil;
      _outputStream = nil;
  
  }
  </code></pre>
  
  <p>Hope this is enough. Thanks in advance</p>