1 /* 2 * Copyright (c) 2000, 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 import java.io.InterruptedIOException; 35 36 public class PortUnreachable implements Runnable { 37 38 DatagramSocket clientSock; 39 int serverPort; 40 int clientPort; 41 42 public void run() { 43 try { 44 InetAddress addr = InetAddress.getLocalHost(); 45 46 Thread.currentThread().sleep(2000); 47 48 // send a delayed packet which should mean a delayed icmp 49 // port unreachable 50 byte b[] = "A late msg".getBytes(); 51 DatagramPacket packet = new DatagramPacket(b, b.length, addr, 52 serverPort); 53 clientSock.send(packet); 54 55 // wait before bringing the server up 56 Thread.currentThread().sleep(5000); 57 58 DatagramSocket sock = new DatagramSocket(serverPort); 59 b = "Grettings from the server".getBytes(); 60 packet = new DatagramPacket(b, b.length, addr, clientPort); 61 sock.send(packet); 62 sock.close(); 63 } catch (Exception e) { 64 e.printStackTrace(); 65 } 66 } 67 68 PortUnreachable() throws Exception { 69 70 clientSock = new DatagramSocket(); 71 clientPort = clientSock.getLocalPort(); 72 73 // pick a port for the server 74 DatagramSocket sock2 = new DatagramSocket(); 75 serverPort = sock2.getLocalPort(); 76 sock2.close(); 77 78 // send a burst of packets to the unbound port - we should get back 79 // icmp port unreachable messages 80 // 81 InetAddress addr = InetAddress.getLocalHost(); 82 byte b[] = "Hello me".getBytes(); 83 DatagramPacket packet = new DatagramPacket(b, b.length, addr, 84 serverPort); 85 for (int i=0; i<100; i++) 86 clientSock.send(packet); 87 88 // start the server thread 89 Thread thr = new Thread(this); 90 thr.start(); 91 92 // try to receive 93 clientSock.setSoTimeout(10000); 94 clientSock.receive(packet); 95 96 // done 97 clientSock.close(); 98 } 99 100 public static void main(String[] args) throws Exception { 101 new PortUnreachable(); 102 } 103 104 }