Skip to content
Snippets Groups Projects
Commit 9eac90c3 authored by Neil's avatar Neil
Browse files

updated the network interface to send content length so we can send arbitrary...

updated the network interface to send content length so we can send arbitrary sizes of data across network
parent dc224876
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -21,15 +21,15 @@ public class MainActivity extends Activity {
network.setNetworkListener( new NetworkResponseListener() {
@Override
public void onData(final byte[] data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView outputView = (TextView) findViewById(R.id.output_field);
String s = "Received " + data.length + " bytes from the server";
Log.d("DEBUG", "update ui: " + data.toString() );
outputView.setText( s );
}
});
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView outputView = (TextView) findViewById(R.id.output_field);
String s = "Received " + data.length + " bytes from the server";
Log.d("DEBUG", "update ui: " + data.toString() );
outputView.setText( s );
}
});
}
});
}
......
......@@ -8,6 +8,7 @@ import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
/**
* Usage:
......@@ -79,6 +80,10 @@ public class NetworkInterface {
try {
Log.d("DEBUG", "Attempt to send: " + data);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
// First we need to write the length
byte[] contentSize = ByteBuffer.allocate(4).putInt(data.length()).array();
out.write( contentSize );
out.flush();
out.write(data.getBytes());
out.flush();
} catch (UnknownHostException e) {
......@@ -110,13 +115,23 @@ public class NetworkInterface {
socket.setKeepAlive(true);
isConnected = true;
byte[] buf = new byte[1024];
InputStream input = socket.getInputStream();
int bytesRead = 0;
while ( (bytesRead = input.read(buf)) > 0 ) {
Log.d("DEBUG", "Recieved " + bytesRead + " bytes from server.");
listener.onData( buf ); // TODO: might need to make a copy of the buffer so it is not overwritten by future reads.
// TODO: this is where we need to convert bytes into whatever we are working with (Mat or just byte[]?)
byte[] contentLengthBuffer = ByteBuffer.allocate(4).array();
// Read 4 bytes of input (corresponding to the content length)
while( input.read(contentLengthBuffer, 0, 4) > 0 ) {
int bytesRead = 0;
int totalBytesRead = 0;
int contentLength = ByteBuffer.wrap(contentLengthBuffer).getInt();
Log.d("DEBUG", "Incoming data of length ["+contentLength+"]");
byte[] content = new byte[contentLength];
// Now we need to read in the contentLength worth of data
while( totalBytesRead < contentLength && (bytesRead = input.read(content)) > 0 ) {
// Just keep reading data
totalBytesRead+= bytesRead;
}
// After the while loop we should have read the entire contents
Log.d("DEBUG", "Recieved ["+contentLength+"] bytes from the server.");
listener.onData( content );
}
} catch (UnknownHostException e1) {
Log.d("DEBUG", e1.toString());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment