title: GCDAsyncSocket on browser connects and disconnects immediately with "Socket closed by remote peer" error
body: <p>I am really new to Network programming and using GCDAsyncSocket for the first time. I am trying to implement an iOS app where 1 device acts a host and other devices(browsers) connect to it. I am using NSNetService/NSNetServiceBrowser for publishing/browsing part.</p>
  
  <p>Now, my client/browser resolves the host's address properly and try to connect to it using following code:</p>
  
  <pre><code>func netServiceDidResolveAddress(sender: NSNetService) {
     // Connect with Service
     if (self.connectWithService(sender, hostname: sender.hostName!)) {
         println("Connecting with service: domainName= \(sender.domain), type= \(sender.type), name= \(sender.name), onPort= \(sender.port) and hostname: \(sender.hostName!)");
     } else {
         println("Unable to connect with service: domainName= \(sender.domain), type= \(sender.type), name= \(sender.name), onPort= \(sender.port)");
     }
  }
  
  private func connectWithService(service: NSNetService, hostname: String) -&gt;Bool {
  
      var isConnecting:Bool = false;
  
  
  
      if (self.browserSocket == nil || !(self.browserSocket?.isConnected)!) {
  
          // Initialize Socket
  
          self.browserSocket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue());
  
  
  
          // Connect
  
          while (!isConnecting){
  
              var error: NSError?;
  
              println("Connecting port : \(service.port) and hostname: \(hostname)");
  
              if (self.browserSocket.connectToHost(hostname, onPort: UInt16(service.port), error: &amp;error)) {
  
                  isConnecting = true;
  
              } else if (error != nil) {
  
                  println("Unable to connect to address. Error \(error) with user info \(error?.userInfo)");
  
              }
  
          }
  
      } else {
  
          println("Connecting is: \(self.browserSocket.isConnected)");
  
          isConnecting = self.browserSocket.isConnected;
  
      }
  
      return isConnecting;
  
  }
  
  // GCDAsyncSocketDelegate methods
  
  func socket(sock: GCDAsyncSocket!, didConnectToHost host: String!, port: UInt16) {
     println("Socket: \(sock) Did connect to host: \(host) on port:\(port)");
  
     // Start Reading
     sock.readDataToLength(UInt(sizeof(UInt64)), withTimeout: -1.0, tag: 0);
  }
  
  func socketDidDisconnect(sock: GCDAsyncSocket!, withError err: NSError!) {
     println("Socket \(sock) did disconnect with error \(err?)");
  
   }
  </code></pre>
  
  <p>Following is the Publisher code:</p>
  
  <p>override func viewDidLoad() {</p>
  
  <pre><code>    super.viewDidLoad()
  
      let barButton = UIBarButtonItem(title: "Stop Publishing", style: .Done, target: self, action: "stopPublishing");
  
      self.navigationItem.rightBarButtonItem = barButton;
  
  
  
      // Publish the service
  
      self.publishService();
  
  }
  
  // MARK: Button callbacks
  
  private func publishService() {
  
      // Initialize GCDAsyncSocket
  
      self.socket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue());
  
  
  
      // Start listening for incoming connections
  
      var error: NSError?;
  
      if (self.socket.acceptOnPort(0, error: &amp;error)) {
  
          // Initialize Service
  
          self.service = NSNetService(domain: "local.", type: "_mpstest._tcp", name: UIDevice.currentDevice().name, port:0);
  
  
  
          // Configure Service
  
          self.service.delegate = self;
  
          self.service.includesPeerToPeer = true;
  
          self.service.publishWithOptions(.ListenForConnections);
  
      } else {
  
          println("Unable to create socket. Error \(error?.description) with user info \(error?.userInfo)");
  
      }
  
  }
  </code></pre>
  
  <p>Now, the client connects with the host and hits following method:</p>
  
  <pre><code>func socket(sock: GCDAsyncSocket!, didConnectToHost host: String!, port: UInt16) {
  </code></pre>
  
  <p>But then it immediately disconnects hitting following method with ErrorCode =7 "Socket closed by remote peer"</p>
  
  <pre><code>func socketDidDisconnect(sock: GCDAsyncSocket!, withError err: NSError!) {
  </code></pre>
  
  <p>Following is the console log from Browser device:</p>
  
  <pre><code>Service found is : &lt;NSNetService 0x15e2f710&gt; local. _mpstest._tcp. iPad 30
  Connecting port : 50797 and hostname: iPad-30.local.
  Connecting with service: domainName= local., type= _mpstest._tcp., name= iPad 30, onPort= 50797 and hostname: iPad-30.local.
  Socket &lt;GCDAsyncSocket: 0x15d80020&gt; did disconnect with error Optional(Error Domain=kCFStreamErrorDomainNetDB Code=8 "nodename nor servname provided, or not known" UserInfo=0x15e3c7a0 {NSLocalizedDescription=nodename nor servname provided, or not known})
  Connecting port : 50797 and hostname: iPad-30.local.
  Connecting with service: domainName= local., type= _mpstest._tcp., name= iPad 30, onPort= 50797 and hostname: iPad-30.local.
  Socket: &lt;GCDAsyncSocket: 0x15e59c20&gt; Did connect to host: 169.254.78.98 on port:50797
  Socket &lt;GCDAsyncSocket: 0x15e59c20&gt; did disconnect with error Optional(Error Domain=GCDAsyncSocketErrorDomain Code=7 "Socket closed by remote peer" UserInfo=0x15e5b070 {NSLocalizedDescription=Socket closed by remote peer})
  </code></pre>
  
  <p>Host socket delegate methods are never hit.</p>
  
  <p>I am really stuck here. Any help as to what am I doing wrong here would be great.</p>
  
  <p>Thanks
  Vik</p>