title: Client-server application with Swift and C++
body: <p>I'm trying to create simple application for transferring data between client(Swift, iOS) and server (C++, Windows). I want to use sockets to transfer data. So i have pretty-much working server (i can connect to it, using c++ client), but when i try to connect to it with Swift's NSStream - i got error. Maybe i'm missing something? What is the rules to use socket connection between c++ server on Windows and iOS.</p>
  
  <p>So after some time, i got "ErrorOccured" message on my client.
  Here's my code to connect with NSStream:</p>
  
  <pre><code>let SERVER_IP: String = "MY_SERVER_IP"
  let SERVER_PORT: Int = MY_PORT
  
  class ViewController: UIViewController, NSStreamDelegate {
      var inputStream: NSInputStream? = nil
      var outputStream: NSOutputStream? = nil
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          NSStream.getStreamsToHostWithName(SERVER_IP, port: SERVER_PORT, inputStream: &amp;inputStream, outputStream: &amp;outputStream)
  
          if inputStream != nil &amp;&amp; outputStream != nil {
              inputStream!.delegate = self
              outputStream!.delegate = self
  
              inputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode)
              outputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode)
  
              inputStream!.open()
              outputStream!.open()
          }
  
       }
  
  
      func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
          switch (eventCode){
          case NSStreamEvent.ErrorOccurred:
              NSLog("ErrorOccurred")
              break
  
          case NSStreamEvent.EndEncountered:
             NSLog("EndEncountered")
             break
  
          case NSStreamEvent.None:
             NSLog("None")
             break
  
          case NSStreamEvent.HasBytesAvailable:
              NSLog("HasBytesAvaible")
              var buffer = [UInt8](count: 4096, repeatedValue: 0)
              if ( aStream == inputStream) {
                  while (inputStream!.hasBytesAvailable){
                      var len = inputStream!.read(&amp;buffer, maxLength: buffer.count)
                      if(len &gt; 0){
                          var output = NSString(bytes: &amp;buffer, length: buffer.count, encoding: NSUTF8StringEncoding)
                          if (output != ""){
                              NSLog("server said: %@", output!)
                          }
                      }
                  }
              }
              break
  
          case NSStreamEvent.allZeros:
              NSLog("allZeros")
              break
  
          case NSStreamEvent.OpenCompleted:
              NSLog("OpenCompleted")
              break
  
          case NSStreamEvent.HasSpaceAvailable:
              NSLog("HasSpaceAvailable")
              break
          default:
              break
        }
  
  
  
      } 
  
   }
  </code></pre>