title: Image streaming from Java to C#
body: <p><strong>Java (client side)</strong></p>
  
  <p>relevant code</p>
  
  <pre><code>    // Create the encoder and decoder for targetEncoding
      Charset charset = Charset.forName("UTF-8");
      CharsetDecoder decoder = charset.newDecoder();
      CharsetEncoder encoder = charset.newEncoder();
      byte[] underlyingBuffer = new byte[100000];
      ByteBuffer buffer = ByteBuffer.wrap(underlyingBuffer);
  
      System.out.println("first buffer remaining" + buffer.remaining() + " "
              + buffer.limit());
      buffer.order(ByteOrder.LITTLE_ENDIAN);
      try {
          Socket client = new Socket("localhost", 8080);
  
          OutputStream oStream = client.getOutputStream();
          InputStream iStream = client.getInputStream();
  
  
          //String secondFilePath = "C:\\Users\\pedge\\Desktop\\readData.csv";//this is csv file
          //long secondFileLength = ReadFileToCharArray(buffer,secondFilePath , encoder);
  
                      String inputImage = "C:\\Users\\pedge\\Desktop\\Desert.jpeg";// image to transfer           
                      ReadFileToCharArray(buffer,inputImage , encoder);
          //imageWrite(oStream, inputImage);
  
          buffer.flip();
  
          int dataToSend = buffer.remaining();
  
          int remaining = dataToSend;
  
          while (remaining &gt; 0) {
              oStream.write(buffer.get());
              --remaining;
          }
  
  public static long ReadFileToCharArray(ByteBuffer buffer, String filePath,
          CharsetEncoder encoder) throws IOException {
              fileCount++;
      System.out.println("second buffer remaining" + buffer.remaining() + " "
              + buffer.limit());
  
      StringBuilder fileData = new StringBuilder(100);
      File file = new File(filePath);
              System.out.println("Size of file ["+fileCount+"] sent is ["+file.length()+"]");
  
      BufferedReader reader = new BufferedReader(new FileReader(filePath));
  
      char[] buf = new char[10000000];
      int numRead = 0;
      while ((numRead = reader.read(buf)) != -1) {
          System.out.println(numRead);
          String readData = String.valueOf(buf, 0, numRead);
          fileData.append(readData);
          buf = new char[1000000];
      }
  
      reader.close();
      CharBuffer charBuffer = CharBuffer.wrap(fileData.toString()
              .toCharArray());
      System.out.println("char buffer " + charBuffer.remaining());
  
      ByteBuffer nbBuffer = null;
      try {
          nbBuffer = encoder.encode(charBuffer);
  
      } catch (CharacterCodingException e) {
          throw new ArithmeticException();
      }
  
      buffer.putInt(nbBuffer.limit());
      System.out.println("buffer information" + buffer.position() + " "
              + buffer.limit() + " nbBuffer information" + nbBuffer.position()
              + " " + nbBuffer.limit());
      buffer.put(nbBuffer);
      return file.length();
  }
  </code></pre>
  
  <p><strong>C# (Server Side)</strong></p>
  
  <pre><code>        static void Main(string[] args)
      {
          try
          {
              Run(args);
          }
          catch (Exception e)
          {
              Console.Error.WriteLine(e);
          }
  
      }
  
     static void Run(string[] args)
      {
          TcpListener listener = new TcpListener(8080);
          listener.Start();
  
          while (true)
          {
              using (TcpClient client = listener.AcceptTcpClient())
              {
                  try
                  {
                      Read(client);
                  }
                  catch (Exception e)
                  {
                      Console.Error.WriteLine(e);
                  }
              }
          }
      }
  static void Read(TcpClient client)
      {
          String csvFile4Parsing = "C:\\Users\\pedge\\Desktop\\newReadData.csv";// csv file created from byte stream
  
          Console.WriteLine("Got connection: {0}", DateTime.Now);
          NetworkStream ns = client.GetStream();
          BinaryReader reader = new BinaryReader(ns);
  
  
          int length = reader.ReadInt32();
          Console.WriteLine("Length of csv is [" + length + "]");
          byte[] fileArray = reader.ReadBytes(length);
          File.WriteAllBytes(csvFile4Parsing, fileArray);//bytes from stream, written to csv`
  </code></pre>
  
  <p>Now i am able to transfer the image from java to C# and a file is then created by c#(works perfect)</p>
  
  <p>But when i try to send the image(same as like CSV), it doesn't work at all and even the bytes received at the server end are different from that of client end. I have spent hours now on this with no success. :/</p>
  
  <p>and help will be much appreciated. thanks.</p>