ConnectedSend.java revision 9330:8b1f1c2a400f
1/*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 * @bug 4849277 7183800
26 * @summary Test DatagramChannel send while connected
27 * @author Mike McCloskey
28 */
29
30import java.io.*;
31import java.net.*;
32import java.nio.*;
33import java.nio.channels.*;
34import java.nio.charset.*;
35
36public class ConnectedSend {
37
38    public static void main(String[] args) throws Exception {
39        test1();
40        test2();
41    }
42
43    // Check if DatagramChannel.send while connected can include
44    // address without throwing
45    private static void test1() throws Exception {
46
47        DatagramChannel sndChannel = DatagramChannel.open();
48        sndChannel.socket().bind(null);
49        InetAddress address = InetAddress.getLocalHost();
50        if (address.isLoopbackAddress()) {
51            address = InetAddress.getLoopbackAddress();
52        }
53        InetSocketAddress sender = new InetSocketAddress(
54            address,
55            sndChannel.socket().getLocalPort());
56
57        DatagramChannel rcvChannel = DatagramChannel.open();
58        rcvChannel.socket().bind(null);
59        InetSocketAddress receiver = new InetSocketAddress(
60            address,
61            rcvChannel.socket().getLocalPort());
62
63        rcvChannel.connect(sender);
64        sndChannel.connect(receiver);
65
66        ByteBuffer bb = ByteBuffer.allocate(256);
67        bb.put("hello".getBytes());
68        bb.flip();
69        int sent = sndChannel.send(bb, receiver);
70        bb.clear();
71        rcvChannel.receive(bb);
72        bb.flip();
73        CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
74        if (!cb.toString().startsWith("h"))
75            throw new RuntimeException("Test failed");
76
77        rcvChannel.close();
78        sndChannel.close();
79    }
80
81    // Check if the datagramsocket adaptor can send with a packet
82    // that has not been initialized with an address; the legacy
83    // datagram socket will send in this case
84    private static void test2() throws Exception {
85        DatagramChannel sndChannel = DatagramChannel.open();
86        sndChannel.socket().bind(null);
87        InetAddress address = InetAddress.getLocalHost();
88        if (address.isLoopbackAddress()) {
89            address = InetAddress.getLoopbackAddress();
90        }
91        InetSocketAddress sender = new InetSocketAddress(
92            address,
93            sndChannel.socket().getLocalPort());
94
95        DatagramChannel rcvChannel = DatagramChannel.open();
96        rcvChannel.socket().bind(null);
97        InetSocketAddress receiver = new InetSocketAddress(
98            address,
99            rcvChannel.socket().getLocalPort());
100
101        rcvChannel.connect(sender);
102        sndChannel.connect(receiver);
103
104        byte b[] = "hello".getBytes("UTF-8");
105        DatagramPacket pkt = new DatagramPacket(b, b.length);
106        sndChannel.socket().send(pkt);
107
108        ByteBuffer bb = ByteBuffer.allocate(256);
109        rcvChannel.receive(bb);
110        bb.flip();
111        CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
112        if (!cb.toString().startsWith("h"))
113            throw new RuntimeException("Test failed");
114
115        // Check that the pkt got set with the target address;
116        // This is legacy behavior
117        if (!pkt.getSocketAddress().equals(receiver))
118            throw new RuntimeException("Test failed");
119
120        rcvChannel.close();
121        sndChannel.close();
122    }
123}
124