1 /*
   2  * Copyright (c) 2000, 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 /**
  25  * @test
  26  * @bug 4361783
  27  * @summary  Test to see if ICMP Port Unreachable on non-connected
  28  *           DatagramSocket causes a SocketException "socket closed"
  29  *           exception on Windows 2000.
  30  */
  31 import java.net.InetAddress;
  32 import java.net.DatagramSocket;
  33 import java.net.DatagramPacket;
  34 
  35 public class PortUnreachable {
  36 
  37     DatagramSocket clientSock;
  38     int serverPort;
  39     int clientPort;
  40 
  41     public void serverSend() {
  42         try {
  43             InetAddress addr = InetAddress.getLocalHost();
  44             Thread.currentThread().sleep(1000);
  45             // send a delayed packet which should mean a delayed icmp
  46             // port unreachable
  47             byte b[] = "A late msg".getBytes();
  48             DatagramPacket packet = new DatagramPacket(b, b.length, addr,
  49                                                        serverPort);
  50             clientSock.send(packet);
  51 
  52             DatagramSocket sock = new DatagramSocket(serverPort);
  53             b = "Greetings from the server".getBytes();
  54             packet = new DatagramPacket(b, b.length, addr, clientPort);
  55             sock.send(packet);
  56             sock.close();
  57         } catch (Exception e) {
  58             e.printStackTrace();
  59         }
  60     }
  61 
  62     PortUnreachable() throws Exception {
  63 
  64         clientSock = new DatagramSocket();
  65         clientPort = clientSock.getLocalPort();
  66 
  67     }
  68     
  69     void execute () throws Exception{
  70 
  71         // pick a port for the server
  72         DatagramSocket sock2 = new DatagramSocket();
  73         serverPort = sock2.getLocalPort();
  74 
  75         // send a burst of packets to the unbound port - we should get back
  76         // icmp port unreachable messages
  77         //
  78         InetAddress addr = InetAddress.getLocalHost();
  79         byte b[] = "Hello me".getBytes();
  80         DatagramPacket packet = new DatagramPacket(b, b.length, addr,
  81                                                    serverPort);
  82         //close just before sending
  83         sock2.close();
  84         for (int i=0; i<100; i++)
  85             clientSock.send(packet);
  86 
  87         serverSend();
  88         // try to receive
  89         b = new byte[25];
  90         packet = new DatagramPacket(b, b.length, addr, serverPort);
  91         clientSock.setSoTimeout(10000);
  92         clientSock.receive(packet);
  93         System.out.println("client received data packet " + new String(packet.getData()));
  94 
  95         // done
  96         clientSock.close();
  97     }
  98 
  99     public static void main(String[] args) throws Exception {
 100         PortUnreachable test = new PortUnreachable();
 101         test.execute();
 102     }
 103 
 104 }