changeset 2620:6c188df7bfef

4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info Reviewed-by: chegar
author alanb
date Fri, 18 Jun 2010 16:16:51 +0100
parents 006e852b692e
children 7526d0b9aab0
files src/share/classes/sun/nio/ch/DatagramSocketAdaptor.java test/java/nio/channels/DatagramChannel/AdaptDatagramSocket.java
diffstat 2 files changed, 15 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/nio/ch/DatagramSocketAdaptor.java	Thu Jun 17 14:42:30 2010 -0700
+++ b/src/share/classes/sun/nio/ch/DatagramSocketAdaptor.java	Fri Jun 18 16:16:51 2010 +0100
@@ -171,10 +171,9 @@
 
     // Must hold dc.blockingLock()
     //
-    private void receive(ByteBuffer bb) throws IOException {
+    private SocketAddress receive(ByteBuffer bb) throws IOException {
         if (timeout == 0) {
-            dc.receive(bb);
-            return;
+            return dc.receive(bb);
         }
 
         // Implement timeout with a selector
@@ -183,8 +182,9 @@
         dc.configureBlocking(false);
         try {
             int n;
-            if (dc.receive(bb) != null)
-                return;
+            SocketAddress sender;
+            if ((sender = dc.receive(bb)) != null)
+                return sender;
             sel = Util.getTemporarySelector(dc);
             sk = dc.register(sel, SelectionKey.OP_READ);
             long to = timeout;
@@ -194,8 +194,8 @@
                 long st = System.currentTimeMillis();
                 int ns = sel.select(to);
                 if (ns > 0 && sk.isReadable()) {
-                    if (dc.receive(bb) != null)
-                        return;
+                    if ((sender = dc.receive(bb)) != null)
+                        return sender;
                 }
                 sel.selectedKeys().remove(sk);
                 to -= System.currentTimeMillis() - st;
@@ -222,7 +222,8 @@
                     ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                     p.getOffset(),
                                                     p.getLength());
-                    receive(bb);
+                    SocketAddress sender = receive(bb);
+                    p.setSocketAddress(sender);
                     p.setLength(bb.position() - p.getOffset());
                 }
             } catch (IOException x) {
--- a/test/java/nio/channels/DatagramChannel/AdaptDatagramSocket.java	Thu Jun 17 14:42:30 2010 -0700
+++ b/test/java/nio/channels/DatagramChannel/AdaptDatagramSocket.java	Fri Jun 18 16:16:51 2010 +0100
@@ -22,6 +22,7 @@
  */
 
 /* @test
+ * @bug 4313882 4981129
  * @summary Unit test for datagram-socket-channel adaptors
  * @library ..
  */
@@ -93,6 +94,11 @@
             if (ip.getData()[ip.getOffset() + i]
                 != op.getData()[op.getOffset() + i])
                 throw new Exception("Incorrect data received");
+
+        if (!(ip.getSocketAddress().equals(dst))) {
+            throw new Exception("Incorrect sender address, expected: " + dst
+                + " actual: " + ip.getSocketAddress());
+        }
     }
 
     static void test(InetSocketAddress dst,