6

At the moment I'm trying to create a little socket application for my Android device.

I would like to send large files (~500MB) via a socket connection to my Laptop/PC or whatever. I'm using 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 (~460MB) my app crashes and it says:

"Throwing OutOfMemoryError "Failed to allocate a 441616290 byte allocationwith 4194304 free bytes and 90MB until OOM""

I guess my client cannot handle this file-size. So my question: is there a way to handle such big files with a TCP socket connection?. My Code works fine with little files (e.g 5MB) but it fails with bigger files.

This is what I have so far:

Client side 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.

4
  • 1
    You need to break your file up into smaller files, often called chunks. Here is a example, in C: stackoverflow.com/questions/1628587/… Commented Jul 15, 2015 at 16:33
  • 1
    Don't read the entire file in at once, do it in smaller chunks. Commented Jul 15, 2015 at 16:34
  • Ok thank you for that info i need some time to look into this code because i am no familiar with c. Commented Jul 15, 2015 at 16:36
  • Just use the same code for receiving that you're using for sending. No need to read the entire input into memory before you write it to the file. Commented Nov 14, 2016 at 4:45

1 Answer 1

5

Here is sample code for Java on how to chunk a file up. You can adopt this to your TCP/IP client-server application. More on chunking is available here (Java - Read file by chunks?), where I took the 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)
{
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

ok that helped me i found my mistake and it is sending the file to my server. Tanks a lot to all of you :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.