Remove flakiness from OldSocketChannelTest#test_socketChannel_read_DirectByteBuffer.

Bug: 6744638
Change-Id: Ic0cf7d51d9aa709e19d611b1cee27771487ed55e
diff --git a/luni/src/test/java/libcore/java/nio/channels/OldSocketChannelTest.java b/luni/src/test/java/libcore/java/nio/channels/OldSocketChannelTest.java
index f182375..3987194 100644
--- a/luni/src/test/java/libcore/java/nio/channels/OldSocketChannelTest.java
+++ b/luni/src/test/java/libcore/java/nio/channels/OldSocketChannelTest.java
@@ -287,17 +287,27 @@
     }
 
     public void test_socketChannel_read_DirectByteBuffer() throws InterruptedException, IOException {
+        final ServerSocketChannel ssc = ServerSocketChannel.open();
+        ssc.socket().bind(null, 0);
 
-        ServerThread server = new ServerThread();
+        Thread server = new Thread() {
+            @Override public void run() {
+                try {
+                    for (int i = 0; i < 2; ++i) {
+                        ByteBuffer buf = ByteBuffer.allocate(10);
+                        buf.put(data);
+                        buf.rewind();
+                        ssc.accept().write(buf);
+                    }
+                } catch (Exception ignored) {
+                }
+            }
+        };
         server.start();
-        Thread.currentThread().sleep(1000);
-
-        InetSocketAddress address = new InetSocketAddress(InetAddress
-                .getByName("localhost"), port);
 
         // First test with array based byte buffer
         SocketChannel sc = SocketChannel.open();
-        sc.connect(address);
+        sc.connect(ssc.socket().getLocalSocketAddress());
 
         ByteBuffer buf = ByteBuffer.allocate(data.length);
         buf.limit(data.length / 2);
@@ -313,7 +323,7 @@
 
         // Now test with direct byte buffer
         sc = SocketChannel.open();
-        sc.connect(address);
+        sc.connect(ssc.socket().getLocalSocketAddress());
 
         buf = ByteBuffer.allocateDirect(data.length);
         buf.limit(data.length / 2);
@@ -339,32 +349,8 @@
     }
 
     public static boolean done = false;
-    public static int port = Support_PortManager.getNextPort();
     public static byte[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 
-    static class ServerThread extends Thread {
-        @Override
-        public void run() {
-            try {
-                ServerSocketChannel ssc = ServerSocketChannel.open();
-                InetSocketAddress addr = new InetSocketAddress(InetAddress
-                        .getByAddress(new byte[] {0, 0, 0, 0}), port);
-                ssc.socket().bind(addr, 0);
-
-                ByteBuffer buf = ByteBuffer.allocate(10);
-                buf.put(data);
-
-                while (!done) {
-                    SocketChannel sc = ssc.accept();
-                    buf.rewind();
-                    sc.write(buf);
-                }
-            } catch (Exception e) {
-                // ignore
-            }
-        }
-    }
-
     class MockSocketChannel extends SocketChannel {
 
         private boolean isConstructorCalled = false;