author | rriggs |
Fri, 02 Feb 2018 14:17:07 -0500 | |
changeset 48737 | 7c12219870fd |
parent 47216 | 71c04702a3d5 |
child 57686 | 70f5cbb711a9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1997, 2018, 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 |
||
24 |
/* @test |
|
25 |
* |
|
26 |
* @bug 4095393 |
|
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
27 |
* @test main SendSize |
2 | 28 |
* @summary this tests a regression in 1.2, beta 2 and earlier where |
29 |
* the DatagramPackets sent the entire buffer, not the buffer length |
|
30 |
* of the packet. |
|
31 |
* |
|
32 |
* @author Benjamin Renaud |
|
33 |
*/ |
|
34 |
||
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
35 |
import java.io.IOException; |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
36 |
import java.net.DatagramPacket; |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
37 |
import java.net.DatagramSocket; |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
38 |
import java.net.InetAddress; |
2 | 39 |
|
40 |
public class SendSize { |
|
41 |
static final int bufferLength = 512; |
|
42 |
static final int packetLength = 256; |
|
43 |
||
44 |
public static void main(String[] args) throws Exception { |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
45 |
DatagramSocket serverSocket = new DatagramSocket(); |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
46 |
Thread server = new ServerThread(serverSocket); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
47 |
server.start(); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
48 |
Thread client = new ClientThread(serverSocket.getLocalPort()); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
49 |
client.start(); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
50 |
server.join(); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
51 |
client.join(); |
2 | 52 |
} |
53 |
||
54 |
static class ServerThread extends Thread { |
|
55 |
DatagramSocket server; |
|
56 |
||
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
57 |
ServerThread(DatagramSocket server) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
58 |
this.server = server; |
2 | 59 |
} |
60 |
||
61 |
public void run() { |
|
62 |
try { |
|
63 |
System.err.println("started server thread: " + server); |
|
64 |
byte[] buf = new byte[1024]; |
|
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
65 |
for (int i = 0; i < 10; i++) { |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
66 |
DatagramPacket receivePacket = new DatagramPacket(buf, |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
67 |
buf.length); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
68 |
server.receive(receivePacket); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
69 |
int len = receivePacket.getLength(); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
70 |
switch (len) { |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
71 |
case packetLength: |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
72 |
System.out.println("receive length is: " + len); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
73 |
break; |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
74 |
default: |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
75 |
throw new RuntimeException( |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
76 |
"receive length is: " + len + |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
77 |
", send length: " + packetLength + |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
78 |
", buffer length: " + buf.length); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
79 |
} |
2 | 80 |
} |
81 |
return; |
|
82 |
} catch (Exception e) { |
|
83 |
e.printStackTrace(); |
|
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
84 |
throw new RuntimeException("caught: " + e); |
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
85 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
86 |
if (server != null) { server.close(); } |
2 | 87 |
} |
88 |
} |
|
89 |
} |
|
90 |
||
91 |
static class ClientThread extends Thread { |
|
92 |
||
93 |
int serverPort; |
|
94 |
DatagramSocket client; |
|
95 |
InetAddress host; |
|
96 |
||
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
97 |
ClientThread(int serverPort)throws IOException { |
2 | 98 |
this.serverPort = serverPort; |
99 |
this.host = InetAddress.getLocalHost(); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
100 |
this.client = new DatagramSocket(); |
2 | 101 |
} |
102 |
||
103 |
public void run() { |
|
104 |
try { |
|
105 |
System.err.println("started client thread: " + client); |
|
106 |
byte[] buf = new byte[bufferLength]; |
|
107 |
DatagramPacket sendPacket = |
|
108 |
new DatagramPacket(buf, packetLength, |
|
109 |
host, serverPort); |
|
110 |
for (int i = 0; i < 10; i++) { |
|
111 |
client.send(sendPacket); |
|
112 |
} |
|
113 |
System.err.println("sent 10 packets"); |
|
114 |
return; |
|
115 |
} catch (Exception e) { |
|
116 |
e.printStackTrace(); |
|
117 |
throw new RuntimeException("caught: " + e); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
118 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
119 |
if (client != null) { client.close(); } |
2 | 120 |
} |
121 |
} |
|
122 |
} |
|
123 |
} |