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.util.concurrent.CountDownLatch; 35 import java.io.InterruptedIOException; 36 37 public class PortUnreachable implements Runnable { 38 39 DatagramSocket clientSock; 40 int serverPort; 41 int clientPort; 42 CountDownLatch clientReadyLatch; 43 44 public void run() { 45 try { 46 InetAddress addr = InetAddress.getLocalHost(); 47 Thread.currentThread().sleep(1000); 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 DatagramSocket sock = new DatagramSocket(serverPort); 56 b = "Grettings from the server".getBytes(); 57 packet = new DatagramPacket(b, b.length, addr, clientPort); 58 clientReadyLatch.await(); 59 sock.send(packet); 60 sock.close(); 61 } catch (Exception e) { 62 e.printStackTrace(); 63 } 64 } 65 66 PortUnreachable() throws Exception { 67 68 clientSock = new DatagramSocket(); 69 clientPort = clientSock.getLocalPort(); 70 clientReadyLatch = new CountDownLatch(1); 71 72 // pick a port for the server 73 DatagramSocket sock2 = new DatagramSocket(); 74 serverPort = sock2.getLocalPort(); 75 76 // send a burst of packets to the unbound port - we should get back 77 // icmp port unreachable messages 78 // 79 InetAddress addr = InetAddress.getLocalHost(); 80 byte b[] = "Hello me".getBytes(); 81 DatagramPacket packet = new DatagramPacket(b, b.length, addr, 82 serverPort); 83 //close just before sending 84 sock2.close(); 85 for (int i=0; i<100; i++) 86 clientSock.send(packet); 87 88 // start the server thread 89 Thread serverSenderThread = new Thread(this); 90 serverSenderThread.start(); 91 92 // try to receive 93 clientSock.setSoTimeout(10000); 94 clientReadyLatch.countDown(); 95 clientSock.receive(packet); 96 System.out.println("client received data packet " + new String(packet.getData())); 97 98 // done 99 clientSock.close(); 100 } 101 102 public static void main(String[] args) throws Exception { 103 new PortUnreachable(); 104 } 105 106 }