title: Working with StreamDelegate/InputStream
body: <p>I'm trying to make a client application in Swift and I've chosen to follow this tutorial as an inspiration:
  <a href="https://www.raywenderlich.com/157128/real-time-communication-streams-tutorial-ios" rel="nofollow noreferrer">https://www.raywenderlich.com/157128/real-time-communication-streams-tutorial-ios</a></p>
  
  <p>I send binary data to the output stream and my server gets the request successfully and answers back. Unfortunately, the Swift client application's StreamDelegate is never triggered.</p>
  
  <p>I've developed in the Playgrounds a simple example of a HTTP request similar to what my application is doing and the same problem occurs:</p>
  
  <pre><code>import UIKit
  
  class WebServerConnection: NSObject, StreamDelegate {
      let host: String
      var inputStream: InputStream!
      var outputStream: OutputStream!
      var responseHandler:((String) -&gt; Void)?
  
      init(_ host:String) {
          self.host = host
      }
  
      func setup(responseHandler:((String) -&gt; Void)?) {
          self.responseHandler = responseHandler
          var readStream: Unmanaged&lt;CFReadStream&gt;?
          var writeStream: Unmanaged&lt;CFWriteStream&gt;?
          CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host as CFString, 80, &amp;readStream, &amp;writeStream)
          inputStream = readStream!.takeRetainedValue()
          outputStream = writeStream!.takeRetainedValue()
          inputStream.delegate = self
          inputStream.schedule(in: .current, forMode: .commonModes)
          outputStream.schedule(in: .current, forMode: .commonModes)
          inputStream.open()
          outputStream.open()
      }
  
      func query(_ path:String = "/") {
          let data = "GET \(path) HTTP/1.1\r\nhost: \(host)\r\n\r\n".data(using: .utf8)!
          _ = data.withUnsafeBytes { outputStream.write($0, maxLength: data.count) }
          print("sent!")
      }
  
      func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
          switch eventCode {
          case Stream.Event.hasBytesAvailable:
              printAvailableBytes(stream: aStream as! InputStream)
          case Stream.Event.endEncountered:
              print("&lt;End Encountered&gt;")
          case Stream.Event.errorOccurred:
              print("&lt;Error occurred&gt;")
          default:
              print("&lt;Some other event&gt;")
          }
      }
  
      func printAvailableBytes(stream: InputStream) {
          let buffer = UnsafeMutablePointer&lt;UInt8&gt;.allocate(capacity: 4096)
          while stream.hasBytesAvailable {
              let numberOfBytesRead = inputStream.read(buffer, maxLength: 4096)
              if numberOfBytesRead &lt; 0 {
                  if stream.streamError != nil {
                      break
                  }
              }
  
              if let message = String(bytesNoCopy: buffer, length: numberOfBytesRead, encoding: .utf8, freeWhenDone: true) {
                  responseHandler?(message)
              }
          }
      }
  
      func close() {
          inputStream.close()
          outputStream.close()
      }
  }
  
  let google = WebServerConnection("google.com")
  google.setup() { message in
      print(message)
      google.close()
  }
  google.query()
  </code></pre>
  
  <p>I get the "sent!" message in the console, but nothing else! I thought with the <code>input.delegate = self</code>, the stream function would be called with the response. What am I missing?</p>