Hello everyone,
My aim is to send a video file from android mobile to matlab for processing. I'm running the server in matlab and android studio is the client.
The connection is being established. But the server is unable to read the data and I'm getting the following error:
error using icinterface/fread (line 160) size must be greater than 0.
Here's the matlab server code:
clc;
close all;
t1 = tcpip('0.0.0.0', 30000, 'NetworkRole', 'server');
disp('waiting for connection');
fopen(t1);
disp('Connection OK');
pause(30);
data = fread(t1, t1.BytesAvailable);
disp('data reading finished');
fclose(t1);
disp('end connection');
Here's the client code i wrote in android studio (using thread concept and sockets)
class Thread2 implements Runnable {
@Override
public void run() {
//
try {
f = new File(videopath);
socket = new Socket(SERVER_IP, SERVER_PORT);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
output = socket.getOutputStream();
input = new FileInputStream(f);
byte[] videoBytes = null;
byte[] bytes = new byte[(int) f.length()];
int n;
while (-1 != (n = input.read(bytes)))
baos.write(bytes, 0, n);
videoBytes = baos.toByteArray();
output.write(videoBytes);
output.flush();
} catch (Exception ex) {
} finally {
try {
if (input != null) input.close();
socket.close();
} catch (Exception exc) {
}
}
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Thread 2 executing..", Toast.LENGTH_SHORT).show();
}
});
}
}
How to resolve this? Am I doing something wrong in the server or client code?