title: get file in InputStream from socket Swift IOS
body: <p>i Want to get file in Input Stream From Socket In swift </p>
  
  <p>There are many examples to get a string from the socket but did not find any method to get a socket file</p>
  
  <pre><code>// To connect to the server
  func connect(host: String, port: Int) {
  
      SwifterHandler.sharedInstance.FilePathSave = SwifterHandler.sharedInstance.rootDoc()
  
      self.host = host
      self.port = port
  
  
      Stream.getStreamsToHost(withName: host, port: port, inputStream: &amp;self.inputStream, outputStream: &amp;self.outputStream)
  
  
      if self.inputStream != nil &amp;&amp; self.outputStream != nil {
          self.inputStream!.delegate = self
          self.outputStream!.delegate = self
          self.inputStream!.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
          self.outputStream!.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
  
  
          self.inputStream!.open()
          self.outputStream!.open()
      }
  
  }
  
  
  // This is a call back func that takes care of I/O from/to the server.
  // Read about this func. There are many event codes to handle.
  // I just wanted to make it simple.
  func stream(aStream: Stream, handleEvent eventCode: Stream.Event) {
      print("Event : \(eventCode)")
      if aStream != inputStream {
          return
      }
  
      if eventCode == .hasBytesAvailable {
          self.ReadFile()
      }
  }
  
  private func ReadFile()
  {
      var buffer = [UInt8](repeating: 0, count: 1024)
      if let fh = FileHandle(forWritingAtPath: "\(SwifterHandler.sharedInstance.FilePathSave)/test.png") {
          fh.seekToEndOfFile()
      while (self.inputStream!.hasBytesAvailable){
  
          let bytesRead: Int = inputStream!.read(&amp;buffer, maxLength: buffer.count)
  
          if bytesRead &gt;= 0 {
  
  
  
               fh.write(Data(bytes: buffer))
  
          }
      }
  
  
  
          fh.closeFile()
      }
      SwifterHandler.sharedInstance.SharedFile(path: SwifterHandler.sharedInstance.FilePathSave)
      self.dataReadCallback!("Success Download")
      InstallApp().InstallApplication()
      self.dataReadCallback!("Application Installed")
  
  }
  </code></pre>
  
  <p>ReadFile() do not run when server send file</p>