1 /* 2 * Copyright (c) 2003, 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 26 * @summary Test DatagramChannel send while connected 27 * @author Mike McCloskey 28 */ 29 30 import java.io.*; 31 import java.net.*; 32 import java.nio.*; 33 import java.nio.channels.*; 34 import java.nio.charset.*; 35 36 public 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 InetSocketAddress sender = new InetSocketAddress( 50 sndChannel.socket().getLocalAddress(), 51 sndChannel.socket().getLocalPort()); 52 53 DatagramChannel rcvChannel = DatagramChannel.open(); 54 rcvChannel.socket().bind(null); 55 InetSocketAddress receiver = new InetSocketAddress( 56 rcvChannel.socket().getLocalAddress(), 57 rcvChannel.socket().getLocalPort()); 58 59 rcvChannel.connect(sender); 60 sndChannel.connect(receiver); 61 62 ByteBuffer bb = ByteBuffer.allocate(256); 63 bb.put("hello".getBytes()); 64 bb.flip(); 65 int sent = sndChannel.send(bb, receiver); 66 bb.clear(); 67 rcvChannel.receive(bb); 68 bb.flip(); 69 CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb); 70 if (!cb.toString().startsWith("h")) 71 throw new RuntimeException("Test failed"); 72 73 rcvChannel.close(); 74 sndChannel.close(); 75 } 76 77 // Check if the datagramsocket adaptor can send with a packet 78 // that has not been initialized with an address; the legacy 79 // datagram socket will send in this case 80 private static void test2() throws Exception { 81 DatagramChannel sndChannel = DatagramChannel.open(); 82 sndChannel.socket().bind(null); 83 InetSocketAddress sender = new InetSocketAddress( 84 sndChannel.socket().getLocalAddress(), 85 sndChannel.socket().getLocalPort()); 86 87 DatagramChannel rcvChannel = DatagramChannel.open(); 88 rcvChannel.socket().bind(null); 89 InetSocketAddress receiver = new InetSocketAddress( 90 rcvChannel.socket().getLocalAddress(), 91 rcvChannel.socket().getLocalPort()); 92 93 rcvChannel.connect(sender); 94 sndChannel.connect(receiver); 95 96 byte b[] = "hello".getBytes("UTF-8"); 97 DatagramPacket pkt = new DatagramPacket(b, b.length); 98 sndChannel.socket().send(pkt); 99 100 ByteBuffer bb = ByteBuffer.allocate(256); 101 rcvChannel.receive(bb); 102 bb.flip(); 103 CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb); 104 if (!cb.toString().startsWith("h")) 105 throw new RuntimeException("Test failed"); 106 107 // Check that the pkt got set with the target address; 108 // This is legacy behavior 109 if (!pkt.getSocketAddress().equals(receiver)) 110 throw new RuntimeException("Test failed"); 111 112 rcvChannel.close(); 113 sndChannel.close(); 114 } 115 }