0

I have been spending forever on this and can not seem to work it out. I am self taught and not very familiar with this so forgive me if it is a remedial question. I am sending data from Android to .Net server. Data is getting corrupt on encoding, I know this I am just not sure how to fix. I am using the .Net Async server sample code found here: Microsoft Async Sample

My Android client code is:

try {
            final Socket sock = new Socket();
            final int timeOut = (int) TimeUnit.SECONDS.toMillis(5); // 5 sec wait period
            sock.connect(new InetSocketAddress("localhost", 11000), timeOut);

            if (sock.isConnected()==true){
                BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));

                String FileName = "myfile.jpg";

                StringBuilder hd = new StringBuilder();
                try {
                    String FilePath= Environment.getExternalStorageDirectory() + "/mydir/" + FileName;
                    File file = new File(FilePath);
                    FileInputStream is = new FileInputStream(file);
                    byte[] chunk = new byte[40960];
                    int chunkLen = 0;
                    while ((chunkLen = is.read(chunk)) != -1) {
                        //String str = new String(Base64.encodeToString(chunk, Base64.NO_WRAP));
                        //String str = new String(chunk, "ASCII");
                        String str = new String(chunk, "UTF-8");
                        out.write(str);
                    }
                    //out.write(hd.toString());
                } catch (FileNotFoundException fnfE) {
                    // file not found, handle case
                } catch (IOException ioE) {
                    // problem reading, handle case
                }

                out.write("<EOF>");
                out.flush();

                StringBuilder returnString = new StringBuilder();
                String line;
                while ((line = in.readLine()) != null) {
                    returnString.append(line).append('\n');
                }

                out.close();
                in.close();
                sock.close();
            }else{
                sock.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

As you can see in my comments, I have tried base64 and UTF-8. I get all kinds of errors on the server when I do that. If I use Base64 I get not part of Base64 error (extra padding etc.). UTF8 writes the file but it is corrupt. When I send it all as one Base64 string it works fine as I use 'Dim data As Byte() = Convert.FromBase64String(FileData)' but as expected it throws memory errors in Android for large files hence the chunking. I am sending some plain ASCII text along with it so I parse out the non-ASCII stuff to write the file. I am super stuck, any help would be much appreciated. Thanks in advance.

nathan
  • 47
  • 4

2 Answers2

0

You don't have to encode it at all. Just write it directly as bytes using an OutputStream. Simpler, quicker, better.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I know you are correct but the server looks for and I also needed to send some other data with the file. I used tags like the so it was just the way I did it. Again, new to this domain so it may end up I change it. Thanks for this though. – nathan Oct 28 '17 at 00:09
  • The server should not look for ``. What if it occurs in the file? See my answer in the duplicate for the correct way to do his. – user207421 Oct 28 '17 at 00:17
-1

I found the answer. It was so weird but makes sense now.

 byte[] chunk = new byte[30000];
                int chunkLen = 0;
                while ((chunkLen = is.read(chunk)) != -1) {
                    String str = new String(Base64.encodeToString(chunk, Base64.NO_WRAP));
                    out.write(str);
                }

I had to change the chunk size to a multiple of 3 then my base64 encoding worked great. Found it here and gave an up vote. Thank you 'mjv'. Link to the answer

nathan
  • 47
  • 4
  • For this to even begin to work it would require the serve to implement an undisclosed base-64 decoding step. – user207421 Oct 28 '17 at 07:57