Sending/receiving .txt file from Android to PC, not whole file is sent
<p>I'm trying to make an Android app that sends a .txt file to a Windows Forms application on my computer. The problem is that not the whole file gets sent (I haven't been able to find out whether the problem is on the sending or receiving side). I only get a random part from somewhere in the middle of the .txt file to the receiving side. What am I doing wrong? The strange thing is that it has worked perfectly a few times, but now I'm never getting the beginning or the end of the file.</p>

<p>The Android app is written in Java, and the Windows Forms app is written in C#. filepath is the name of my file. What is the problem here?</p>

<p><strong>Code for Android app (sending file)</strong></p>

<pre><code>//create new byte array with the same length as the file that is to be sent

byte[] array = new byte[(int) filepath.length()];

FileInputStream fileInputStream = new FileInputStream(filepath);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
//use bufferedInputStream to read to end of file
bufferedInputStream.read(array, 0, array.length);
//create objects for InputStream and OutputStream
//and send the data in array to the server via socket
OutputStream outputStream = socket.getOutputStream();
outputStream.write(array, 0, array.length);
</code></pre>

<p><strong>Code for Windows Forms app (receiving file)</strong></p>

<pre><code>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();
</code></pre>
