blob: 32706f3452102d4a2de8f81cdb95ba3d7635446a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 4669040
26 * @summary Test DatagramChannel subsequent receives with no datagram ready
27 * @author Mike McCloskey
28 */
29
30import java.io.*;
31import java.net.*;
32import java.nio.*;
33import java.nio.channels.*;
34import java.nio.charset.*;
35
36public class Sender {
37
38 static PrintStream log = System.err;
39
40 public static void main(String[] args) throws Exception {
41 test();
42 }
43
44 static void test() throws Exception {
45 Sprintable server = new Server();
46 Sprintable client = new Client();
47
48 Thread serverThread = new Thread(server);
49 serverThread.start();
50 while (!server.ready())
51 Thread.sleep(50);
52
53 Thread clientThread = new Thread(client);
54 clientThread.start();
55
56 serverThread.join();
57 clientThread.join();
58
59 server.throwException();
60 client.throwException();
61 }
62
63 public interface Sprintable extends Runnable {
64 public void throwException() throws Exception;
65 public boolean ready();
66 }
67
68 public static class Client implements Sprintable {
69 Exception e = null;
70
71 public void throwException() throws Exception {
72 if (e != null)
73 throw e;
74 }
75
76 private volatile boolean ready = false;
77
78 public boolean ready() {
79 return ready;
80 }
81
82 public void run() {
83 try {
84 DatagramChannel dc = DatagramChannel.open();
85 ByteBuffer bb = ByteBuffer.allocateDirect(12);
86 bb.order(ByteOrder.BIG_ENDIAN);
87 bb.putInt(1).putLong(1);
88 bb.flip();
89 InetAddress address = InetAddress.getLocalHost();
90 InetSocketAddress isa = new InetSocketAddress(address, 8888);
91 dc.connect(isa);
92 dc.write(bb);
93 } catch (Exception ex) {
94 e = ex;
95 }
96 }
97 }
98
99 public static class Server implements Sprintable {
100 Exception e = null;
101 private volatile boolean ready = false;
102
103 public void throwException() throws Exception {
104 if (e != null)
105 throw e;
106 }
107
108 public boolean ready() {
109 return ready;
110 }
111
112 void showBuffer(String s, ByteBuffer bb) {
113 log.println(s);
114 bb.rewind();
115 for (int i=0; i<bb.limit(); i++) {
116 byte element = bb.get();
117 log.print(element);
118 }
119 log.println();
120 }
121
122 public void run() {
123 SocketAddress sa = null;
124
125 try {
126 DatagramChannel dc = DatagramChannel.open();
127 dc.socket().bind(new InetSocketAddress(8888));
128 dc.configureBlocking(false);
129 ready = true;
130 ByteBuffer bb = ByteBuffer.allocateDirect(12);
131 bb.clear();
132 // Get the one valid datagram
133 while (sa == null)
134 sa = dc.receive(bb);
135 sa = null;
136 for (int i=0; i<100; i++) {
137 bb.clear();
138 sa = dc.receive(bb);
139 if (sa != null)
140 throw new RuntimeException("Test failed");
141 }
142 dc.close();
143 } catch (Exception ex) {
144 e = ex;
145 }
146 }
147 }
148
149}