diff --git a/.DS_Store b/.DS_Store index 40abbb989e9dc24f55fc3c438e8242c8256075db..df3f70a3e423600a1241c28add1b9bee644e2ce1 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/AndroidSockets/app/src/main/java/edu/uw/cs/androidsockets/MainActivity.java b/AndroidSockets/app/src/main/java/edu/uw/cs/androidsockets/MainActivity.java index bb2ec4c71a663f34ab52716e1dac62a17f32ab38..844d1b304cecfef33714fb5e15a199aaee7d6cfa 100644 --- a/AndroidSockets/app/src/main/java/edu/uw/cs/androidsockets/MainActivity.java +++ b/AndroidSockets/app/src/main/java/edu/uw/cs/androidsockets/MainActivity.java @@ -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 ); + } + }); } }); } diff --git a/AndroidSockets/app/src/main/java/edu/uw/cs/androidsockets/NetworkInterface.java b/AndroidSockets/app/src/main/java/edu/uw/cs/androidsockets/NetworkInterface.java index 75dcc0f3af6e4dd5c5a4a294379d5982cc679674..96f8396f980384fce63d147cd5b8179da28dec70 100644 --- a/AndroidSockets/app/src/main/java/edu/uw/cs/androidsockets/NetworkInterface.java +++ b/AndroidSockets/app/src/main/java/edu/uw/cs/androidsockets/NetworkInterface.java @@ -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());