c# - Sending/receiving .txt file from Android to PC, not whole file is sent -


i'm trying make android app sends .txt file windows forms application on computer. problem not whole file gets sent (i haven't been able find out whether problem on sending or receiving side). random part somewhere in middle of .txt file receiving side. doing wrong? strange thing has worked few times, i'm never getting beginning or end of file.

the android app written in java, , windows forms app written in c#. filepath name of file. problem here?

code android app (sending file)

//create new byte array same length file sent  byte[] array = new byte[(int) filepath.length()];  fileinputstream fileinputstream = new fileinputstream(filepath); bufferedinputstream bufferedinputstream = new bufferedinputstream(fileinputstream); //use bufferedinputstream read end of file bufferedinputstream.read(array, 0, array.length); //create objects inputstream , outputstream //and send data in array server via socket outputstream outputstream = socket.getoutputstream(); outputstream.write(array, 0, array.length); 

code windows forms app (receiving file)

tcpclient tcpclient = (tcpclient)client; networkstream clientstream = tcpclient.getstream();  byte[] message = new byte[65535]; int bytesread;  clientstream.read(message, 0, message.length); system.io.filestream fs = system.io.file.create(path + dt); //message has been received asciiencoding encoder = new asciiencoding(); system.diagnostics.debug.writeline(encoder.getstring(message, 0, bytesread));   fs.write(message, 0, bytesread); fs.close(); 

instead of reading complete array memory , sending outputstream afterwards, read/write @ same time, , use "small" buffer byte array. this:

public boolean copystream(inputstream inputstream, outputstream outputstream){     bufferedinputstream bis = new bufferedinputstream(inputstream);     bufferedoutputstream bos = new bufferedoutputstream(outputstream);      byte[] buffer = new byte[4*1024]; //whatever buffersize want use.      try {         int read;         while ((read = bis.read(buffer)) != -1){             bos.write(buffer, 0, read);         }         bos.flush();         bis.close();         bos.close();     } catch (ioexception e) {         //log, retry, cancel, whatever         return false;     }      return true; } 

on receiving side should same: write portion of bytes receive them , not store them completly memory befor using.

this might not fix issue, should improve anyway.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -