1/* 2 * Copyright (c) 2001, 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 4433616 27 * @summary Test that packet discarding is done at impl level 28 */ 29import java.net.*; 30 31public class CheckDiscard { 32 33 CheckDiscard() throws Exception { 34 35 DatagramSocket s = new DatagramSocket(); 36 37 /* 38 * Create two sender threads 39 */ 40 Sender s1 = new Sender( s.getLocalPort() ); 41 Sender s2 = new Sender( s.getLocalPort() ); 42 43 /* 44 * "connect" to sender 1 45 */ 46 InetAddress ia = InetAddress.getLocalHost(); 47 s.connect( ia, s1.getLocalPort() ); 48 49 /* 50 * Kick off the senders 51 */ 52 (new Thread(s1)).start(); 53 (new Thread(s2)).start(); 54 55 /* 56 * Receive packets and verify that they came from the 57 * right sender 58 */ 59 byte b[] = new byte[512]; 60 DatagramPacket p = new DatagramPacket(b, b.length); 61 s.setSoTimeout(4000); 62 try { 63 for (int i=0; i<20; i++) { 64 s.receive(p); 65 if ((p.getPort() != s1.getLocalPort()) || 66 (!p.getAddress().equals(ia))) { 67 throw new Exception("Received packet from wrong sender"); 68 } 69 } 70 } catch (SocketTimeoutException e) { 71 } 72 73 /* 74 * Finally check if either sender threw an exception 75 */ 76 Exception e; 77 e = s1.getException(); 78 if (e != null) throw e; 79 e = s2.getException(); 80 if (e != null) throw e; 81 } 82 83 public static void main(String args[]) throws Exception { 84 new CheckDiscard(); 85 } 86 87 88 89 public class Sender implements Runnable { 90 91 Exception exc = null; 92 DatagramSocket s; 93 int port; 94 95 Sender(int port) throws Exception { 96 s = new DatagramSocket(); 97 this.port = port; 98 } 99 100 public int getLocalPort() { 101 return s.getLocalPort(); 102 } 103 104 public void setException(Exception e) { 105 exc = e; 106 } 107 108 public Exception getException() { 109 return exc; 110 } 111 112 /* 113 * Send 10 packets to the receiver 114 */ 115 public void run() { 116 try { 117 118 byte b[] = "Hello".getBytes(); 119 DatagramPacket p = new DatagramPacket(b, b.length); 120 p.setAddress( InetAddress.getLocalHost() ); 121 p.setPort( port ); 122 123 for (int i=0; i<10; i++) { 124 s.send(p); 125 Thread.currentThread().sleep(1000); 126 } 127 128 } catch (Exception e) { 129 setException(e); 130 } 131 132 s.close(); 133 } 134 } 135 136} 137