3
votes

I want Matlab to perform as a server and Android Java app to perform as a client. Android should transfer a byte array of size 460K to Matlab. I cannot manage to get the entire array correctly on Matlab. The first call to fread in Matlab reads a random number of bytes (around 320K-290K) - this number is correctly presented by t.BytesAvailable. After that t.BytesAvailable gets 0 and no further reading is possible. Here is what I used:

Java client code:

Socket socket = new Socket("10.0.0.2", 3000);
OutputStream out = socket.getOutputStream();
out.write(buffer, 0, 460000);
out.flush();
out.close();
socket.close();

Matlab server code:

t=tcpip('0.0.0.0', 3000, 'NetworkRole', 'server');
set(t, 'InputBufferSize', 500000); 
fopen(t); 
pause(1);
while (get(t, 'BytesAvailable') > 0) 
    display(get(t, 'BytesAvailable'));
    data=fread(t, t.BytesAvailable, 'uint8');
end
fclose(t); 
delete(t); 
clear t 

Matlab version:

>> ver

MATLAB Version: 8.1.0.604 (R2013a)

Operating System: Microsoft Windows 7 Version 6.1 (Build 7601: Service Pack 1)

Java Version: Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode

MATLAB Version 8.1 (R2013a) Computer Vision System Toolbox Version 5.2 (R2013a) Image Acquisition Toolbox Version 4.5 (R2013a) Image Processing Toolbox Version 8.2 (R2013a) Instrument Control Toolbox Version 3.3 (R2013a) MATLAB Coder Version 2.4 (R2013a)

1

1 Answers

1
votes

Sending a large amount of data over TCP and immediately closing the socket right after may be a little "unsafe". Check out this super informative guide - SO_LINGER - and this good answer for a possible clean work around.

A simpler, dirtier, workaround in case you can afford it is simply to wait (with a sleep call) after sending and before socket.close().