Android sends large files via socket

I'm currently trying to create a small socket app for my Android device.

I would like to send large files (~ 500 MB) via a socket connection to my laptop / PC or something else. I use a socket client on my Android device to connect to my socket server on my PC, but when I try to send a test field (~ 460 MB), my application crashes and it says:

"Throwing OutOfMemoryError" Failed to allocate byte 441616290 destination with 4194304 free bytes and 90 MB before OOM ""

I think my client cannot handle this file size. So my question is: is there a way to handle such large files using a TCP socket connection ?. My code works fine with small files (e.g. 5 MB), but it does not work with large files.

This is what I have so far:

The client side is running on my Android device:

private class Connecting extends AsyncTask<String, Integer, String> { @Override protected String doInBackground(String... serverAdd) { String filePath = "Path to file"; File sdFile = new File(filePath); try { client = new Socket("ip", "port"); outputStream = client.getOutputStream(); byte[] buffer = new byte[1024]; FileInputStream in = new FileInputStream(sdFile); int rBytes; while((rBytes = in.read(buffer, 0, 1024)) != -1) { outputStream.write(buffer, 0, rBytes); } outputStream.flush(); outputStream.close(); client.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } } 

Server side:

 public class Main { private static ServerSocket serverSocket; private static Socket clientSocket; private static InputStream inputStream; private static FileOutputStream fileOutputStream; private static BufferedOutputStream bufferedOutputStream; private static int filesize = 10000000; private static int bytesRead; private static int current = 0; public static void main(String[] args) throws IOException { serverSocket = new ServerSocket(10898); System.out.println("Server started. Listening to the port 10898"); clientSocket = serverSocket.accept(); byte[] mybytearray = new byte[filesize]; inputStream = clientSocket.getInputStream(); fileOutputStream = new FileOutputStream("E:\\output.zip"); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); System.out.println("Receiving..."); bytesRead = inputStream.read(mybytearray, 0, mybytearray.length); current = bytesRead; do { bytesRead = inputStream.read(mybytearray, current, (mybytearray.length - current)); if (bytesRead >= 0) { current += bytesRead; } } while (bytesRead > -1); bufferedOutputStream.write(mybytearray, 0, current); bufferedOutputStream.flush(); bufferedOutputStream.close(); inputStream.close(); clientSocket.close(); serverSocket.close(); System.out.println("Sever recieved the file"); } } 

Greetz

[EDIT]: client code.

+5
source share
1 answer

Here is sample code for Java on how to cut a file. You can port this to your TCP / IP client server application. More about chunking can be found here ( Java - read the file in chunks? ), Where I took a sample from.

 char[] myBuffer = new char[1024]; int bytesRead = 0; BufferedReader in = new BufferedReader(new FileReader("foo.txt")); while ((bytesRead = in.read(myBuffer, 0, 1024)) != -1) { ... } 
+3
source

All Articles