author | chegar |
Fri, 18 Oct 2019 17:08:59 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58688 | 2b1e684c3ce6 |
parent 57965 | ef15850629cd |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57965
ef15850629cd
8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
58688
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
24 |
/* |
2 | 25 |
* @test |
26 |
* @bug 4424096 |
|
27 |
* @summary DatagramPacket spec needs clarification (reuse buf) |
|
58688
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
28 |
* @run main ReuseBuf |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
29 |
* @run main/othervm -Djava.net.preferIPv4Stack=true ReuseBuf |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
30 |
* @run main/othervm -Djdk.net.usePlainDatagramSocketImpl=true ReuseBuf |
2 | 31 |
*/ |
32 |
import java.net.*; |
|
58688
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
33 |
import static java.lang.System.out; |
2 | 34 |
|
35 |
public class ReuseBuf { |
|
36 |
static String msgs[] = {"Hello World", "Java", "Good Bye"}; |
|
37 |
static int port; |
|
38 |
||
39 |
static class ServerThread extends Thread{ |
|
40 |
DatagramSocket ds; |
|
41 |
public ServerThread() { |
|
42 |
try { |
|
57965
ef15850629cd
8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents:
47216
diff
changeset
|
43 |
InetAddress local = InetAddress.getLocalHost(); |
ef15850629cd
8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents:
47216
diff
changeset
|
44 |
InetSocketAddress bindaddr = new InetSocketAddress(local, 0); |
ef15850629cd
8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents:
47216
diff
changeset
|
45 |
ds = new DatagramSocket(bindaddr); |
2 | 46 |
port = ds.getLocalPort(); |
47 |
} catch (Exception e) { |
|
48 |
throw new RuntimeException(e.getMessage()); |
|
49 |
} |
|
50 |
} |
|
51 |
||
52 |
public void run() { |
|
53 |
byte b[] = new byte[100]; |
|
54 |
DatagramPacket dp = new DatagramPacket(b,b.length); |
|
55 |
while (true) { |
|
56 |
try { |
|
57 |
ds.receive(dp); |
|
58 |
String reply = new String(dp.getData(), dp.getOffset(), dp.getLength()); |
|
58688
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
59 |
out.println("server: received [" + reply + "]"); |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
60 |
for (int i=0; i<2; i++) { |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
61 |
out.println("server: sending [" + reply + "]"); |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
62 |
ds.send(new DatagramPacket(reply.getBytes(), reply.length(), |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
63 |
dp.getAddress(), dp.getPort())); |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
64 |
} |
2 | 65 |
if (reply.equals(msgs[msgs.length-1])) { |
66 |
break; |
|
67 |
} |
|
68 |
} catch (Exception e) { |
|
69 |
throw new RuntimeException(e.getMessage()); |
|
70 |
} |
|
71 |
} |
|
72 |
ds.close(); |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
public static void main(String args[]) throws Exception { |
|
77 |
ServerThread st = new ServerThread(); |
|
78 |
st.start(); |
|
57965
ef15850629cd
8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents:
47216
diff
changeset
|
79 |
InetAddress local = InetAddress.getLocalHost(); |
ef15850629cd
8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents:
47216
diff
changeset
|
80 |
InetSocketAddress bindaddr = new InetSocketAddress(local, 0); |
ef15850629cd
8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents:
47216
diff
changeset
|
81 |
DatagramSocket ds = new DatagramSocket(bindaddr); |
2 | 82 |
byte b[] = new byte[100]; |
83 |
DatagramPacket dp = new DatagramPacket(b,b.length); |
|
84 |
for (int i = 0; i < msgs.length; i++) { |
|
58688
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
85 |
out.println("client: sending [" + msgs[i] + "]"); |
2 | 86 |
ds.send(new DatagramPacket(msgs[i].getBytes(),msgs[i].length(), |
13359
edb9f5468e9a
7081476: test/java/net/InetSocketAddress/B6469803.java failing intermittently
chegar
parents:
5506
diff
changeset
|
87 |
InetAddress.getLocalHost(), |
2 | 88 |
port)); |
89 |
ds.receive(dp); |
|
58688
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
90 |
String recvmsg = new String(dp.getData(), dp.getOffset(), dp.getLength()); |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
91 |
out.println("client: received [" + recvmsg + "]"); |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
92 |
if (!msgs[i].equals(recvmsg)) { |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
93 |
throw new RuntimeException("Msg expected: " + msgs[i] + |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
94 |
", msg received: " + recvmsg); |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
95 |
} |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
96 |
|
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
97 |
byte ba[] = new byte[1]; |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
98 |
DatagramPacket dp2 = new DatagramPacket(ba, ba.length); |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
99 |
dp2.setData(new byte[99], 5, 3); // only 3 bytes |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
100 |
ds.receive(dp2); |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
101 |
String expectedMsg = msgs[i].substring(0, 3); |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
102 |
recvmsg = new String(dp2.getData(), dp2.getOffset(), dp2.getLength()); |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
103 |
out.println("client: received [" + recvmsg + "]"); |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
104 |
if (!expectedMsg.equals(recvmsg)) { |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
105 |
throw new RuntimeException("Msg truncated expected: " + expectedMsg + |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
106 |
", msg received: "+ recvmsg); |
2 | 107 |
} |
108 |
} |
|
109 |
ds.close(); |
|
58688
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
110 |
out.println("Test Passed!!!"); |
2b1e684c3ce6
datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents:
57965
diff
changeset
|
111 |
st.join(); |
2 | 112 |
} |
113 |
} |