Reputation: 29
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:
MATLAB Version: 8.1.0.604 (R2013a)
Operating System: Microsoft Windows 7 Version 6.1 (Build 7601: Service Pack 1)
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)
Upvotes: 3
Views: 990
Reputation: 1765
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()
.
Upvotes: 1