jdk/test/java/net/DatagramSocket/ChangingAddress.java
changeset 10822 0294e016d9b1
parent 10821 5ec6698ec5a9
parent 10808 882388c78d4e
child 10823 86db042b3385
equal deleted inserted replaced
10821:5ec6698ec5a9 10822:0294e016d9b1
     1 /*
       
     2  * Copyright (c) 2011, 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 7084030
       
    26  * @summary Tests that DatagramSocket.getLocalAddress returns the right local
       
    27  *          address after connect/disconnect.
       
    28  */
       
    29 import java.net.*;
       
    30 
       
    31 public class ChangingAddress {
       
    32 
       
    33     static void check(DatagramSocket ds, InetAddress expected) {
       
    34         InetAddress actual = ds.getLocalAddress();
       
    35         if (!expected.equals(actual)) {
       
    36             throw new RuntimeException("Expected:"+expected+" Actual"+
       
    37                                        actual);
       
    38         }
       
    39     }
       
    40 
       
    41     public static void main(String[] args) throws Exception {
       
    42         InetAddress lh = InetAddress.getLocalHost();
       
    43         SocketAddress remote = new InetSocketAddress(lh, 1234);
       
    44         InetAddress wildcard = InetAddress.getByAddress
       
    45                                ("localhost", new byte[]{0,0,0,0});
       
    46         try (DatagramSocket ds = new DatagramSocket()) {
       
    47             check(ds, wildcard);
       
    48 
       
    49             ds.connect(remote);
       
    50             check(ds, lh);
       
    51 
       
    52             ds.disconnect();
       
    53             check(ds, wildcard);
       
    54        }
       
    55     }
       
    56 }