Effacer les filtres
Effacer les filtres

Error in receiving data from client (using sockets)

11 vues (au cours des 30 derniers jours)
sree dhruti
sree dhruti le 16 Avr 2020
Réponse apportée : arushi le 28 Août 2024
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?

Réponses (1)

arushi
arushi le 28 Août 2024
Hi Sree,
The error you're encountering in MATLAB indicates that the fread function is being called with a size of 0, which suggests that no data is available to read from the TCP/IP connection.
Potential Issues and SolutionsMATLAB Server Code
Reading Data Correctly:
  • The fread function is being called with t1.BytesAvailable, which might be 0 if the data hasn't arrived yet. Instead, you could specify a larger number or use a loop to ensure all data is read.
Waiting for Data:
  • Ensure the server waits until data is available before attempting to read. You can use a loop to check BytesAvailable.
Android Client Code
Ensure Complete Data Transmission:
  • Make sure the entire video file is being sent. The code seems correct, but verify that the file is not too large for the buffer sizes on both client and server.
Flush and Close Properly:
  • Ensure the output stream is flushed and closed properly after sending the data, which you appear to be doing. Also, ensure that exceptions are being logged or printed for debugging purposes.
Network Delays:
  • Consider adding a short delay after establishing the connection before sending data to ensure the server is ready.
Hope this helps.

Catégories

En savoir plus sur Modeling dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by