title: How to send Images over TCP connection in Swift4
body: <p>I am developing mac OS application in that i want to send the images in every 5 seconds to another mac . i am running Node.js TCP server at backend side , and connecting both of them as TCP Client .</p>
  
  <p>I am  trying to send Base64 Encode String to server  and server is pushing data to clients .</p>
  
  <p><strong>Receiving code :</strong></p>
  
  <pre><code>   extension SocketCreator: StreamDelegate {
          func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
              switch eventCode {
              case Stream.Event.hasBytesAvailable:
                  print("new message received")
                  //readAvailableBytes(stream: aStream as! InputStream)
              case Stream.Event.endEncountered:
                  disconnectFromHost()
              case Stream.Event.errorOccurred:
                  print("error occurred  : \(aStream.streamError.debugDescription)")
              case Stream.Event.hasSpaceAvailable:
                  print("has space available")
              default:
                  print("some other event...")
                  break
              }
          }
  
          private func readAvailableBytes(stream: InputStream) {
              let buffer = UnsafeMutablePointer&lt;UInt8&gt;.allocate(capacity: maxReadLength)
  
              while (stream != NSNull.init())&amp;&amp;stream.hasBytesAvailable {
                  let numberOfBytesRead = inputStream.read(buffer, maxLength: maxReadLength)
  
                  if numberOfBytesRead &lt; 0 {
                      if let _ = inputStream.streamError {
                          break
                      }
                  }
  
                  let reciviedMsg = String(bytesNoCopy: buffer,
                                           length: numberOfBytesRead,
                                           encoding: .ascii,
                                           freeWhenDone: true)
  
                  delegate?.scoktDidRecivceNewMessagew(message: reciviedMsg ?? "-NA-")
  
  
              }
          }
  }
  </code></pre>
  
  <p>When i am receiving base64 string in second computer , i and loosing some on bits and base64 is not decodable . what could be the possible reason for this.</p>
  
  <p>if am doing wrong to send data in base64 encode string , what is the best practice to Images over TCP without loosing data .</p>
  
  <p>This is how i am collecting fragments:</p>
  
  <pre><code> func scoktDidRecivceNewMessage(message: String) {
         // print("Recived Command @ cient : \(message)")
          if message.contains("data:image/png;base64,") {
              hasStartedReadingImage = true
              base64ImageFrame = message
          }
  
          if hasStartedReadingImage {
              if message.contains("**end")
              {
                  hasStartedReadingImage = false
                  base64ImageFrame.append(message)
                  var base64 = base64ImageFrame.replacingOccurrences(of: "data:image/jpeg;base64,", with: "")
                  base64 = base64.replacingOccurrences(of: "**end", with: "")
  
                  let nsImage = base64.base64Convert()
                  DispatchQueue.main.async {
                      self.imgStremingView.image = nsImage
                  }
  
  
              }else
              {
                 base64ImageFrame.append(message)
              }
          }
  
      }
  </code></pre>