Error in receiving data from client (using sockets)

3 次查看(过去 30 天)
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');
%disp(char(data));
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?

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Mobile 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by