blob: 5df738a313ed74df45e412748a25540e052a9c77 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001 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/**
25 * @test
26 *
27 * @bug 4424096
28 *
29 * @summary DatagramPacket spec needs clarification (reuse buf)
30 */
31import java.net.*;
32import java.io.*;
33
34public class ReuseBuf {
35 static String msgs[] = {"Hello World", "Java", "Good Bye"};
36 static int port;
37
38 static class ServerThread extends Thread{
39 DatagramSocket ds;
40 public ServerThread() {
41 try {
42 ds = new DatagramSocket();
43 port = ds.getLocalPort();
44 } catch (Exception e) {
45 throw new RuntimeException(e.getMessage());
46 }
47 }
48
49 public void run() {
50 byte b[] = new byte[100];
51 DatagramPacket dp = new DatagramPacket(b,b.length);
52 while (true) {
53 try {
54 ds.receive(dp);
55 String reply = new String(dp.getData(), dp.getOffset(), dp.getLength());
56 ds.send(new DatagramPacket(reply.getBytes(),reply.length(),
57 dp.getAddress(),dp.getPort()));
58 if (reply.equals(msgs[msgs.length-1])) {
59 break;
60 }
61 } catch (Exception e) {
62 throw new RuntimeException(e.getMessage());
63 }
64 }
65 ds.close();
66 }
67 }
68
69 public static void main(String args[]) throws Exception {
70 ServerThread st = new ServerThread();
71 st.start();
72 DatagramSocket ds = new DatagramSocket();
73 byte b[] = new byte[100];
74 DatagramPacket dp = new DatagramPacket(b,b.length);
75 for (int i = 0; i < msgs.length; i++) {
76 ds.send(new DatagramPacket(msgs[i].getBytes(),msgs[i].length(),
77 InetAddress.getByName("LocalHost"),
78 port));
79 ds.receive(dp);
80 if (!msgs[i].equals(new String(dp.getData(), dp.getOffset(), dp.getLength()))) {
81 throw new RuntimeException("Msg expected: "+msgs[i] +msgs[i].length()+
82 "msg received: "+new String(dp.getData(), dp.getOffset(), dp.getLength())+dp.getLength());
83 }
84 }
85 ds.close();
86 System.out.println("Test Passed!!!");
87 }
88}