test/java/net/DatagramSocket/PortUnreachable.java

Print this page




  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 }


  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 }