title: using python to send an image over TCP to c# server
body: <p>I am working on a project that uses a python script to send an image taken with a webcam to a c# webserver using sockets. I am currently sending strings to the server from the python client using code like:</p>
  
  <pre><code>info = bytearray("Text to send", "UTF-8")
  socket.send(info)
  </code></pre>
  
  <p>to send data to the c# server and it functions perfectly for text and numbers. I run into an issue when trying to encode the data read from the .bmp into "UTF-8", as trying this returns an error of not being able to encode certain characters into UTF-8.</p>
  
  <p>I was wondering if anyone had any idea of a way to encode this that c# will be able to recognize, or, if there is a better way of trying to implement this process, I am all ears.</p>
  
  <p>A couple of options I have come up with would be to 1 - use something like google drive to save the image to, or an FTP server and then have the c# server retrieve it from there or 2 - create a packet system containing the RGB values and recreating the image from those pixel values on the server. </p>
  
  <p>Thanks for your help.</p>
  
  <p><strong>EDIT:</strong> I have tried sending the file this way</p>
  
  <pre><code> data = bytearray("123456789","UTF-8")
   file = open("image.bmp", "rb")
   data += file.read()
   socket.send(data)
  </code></pre>
  
  <p>and was able to successfully retreive the string "123456789", but the data after this is garbage. I have also implemented sending the size of the file before sending the data and that size number is retrieved fine, but the img data saves as a black bmp.</p>
  
  <p><strong>Edit 2 :</strong></p>
  
  <p>Here is the server and client code I am using to try and recreate the image using a memory stream. I the client code is using the process mentioned by hcalves.</p>
  
  <p><strong>Client</strong></p>
  
  <pre><code>if __name__ == "__main__":
  sock = socket.socket()
  sock.connect(("localhost", 50839))
  
  with open("image.bmp", "rb") as fd:
      buf = fd.read(1024)
      while (buf):
          sock.send(buf)
          buf = fd.read(1024)
  sock.close()
  </code></pre>
  
  <p><strong>Server</strong></p>
  
  <pre><code>Socket client = server.AcceptSocket();
  NetworkStream stream = new NetworkStream(client);
  byte[] imgData = new byte[1024];
  MemoryStream memstream = new MemoryStream();
  stream.Read(imgData,0, 1024);
  int counter = 0;
  while (stream.DataAvailable)
  {
      memstream.Write(imgData, 0, 1024);
      stream.Read(imgData, 0, 1024);
      counter = counter + 1024;
  }
  
  memstream.Seek(0, SeekOrigin.Begin);
  
  using (Stream file = File.OpenWrite("img.bmp"))
  {
      byte[] buffer = new byte[8*1024];
      int len;
      while ((len = memstream.Read(buffer, 0, buffer.Length)) &gt; 0)
      {
          file.Write(buffer,0,len);
      }
  }
  </code></pre>