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.
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)
{
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With