blob: f728057b4e76e11cab8896917f5770a2d52fe2da [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999 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 4132931
26 * @summary DatagramSocket should use a factory for its impl
27 *
28 * @build ADatagramSocket
29 * @run shell ADatagramSocket.sh
30 */
31import java.io.*;
32import java.net.*;
33import java.util.*;
34
35public class ADatagramSocket {
36 public static void main(String[] args) throws IOException {
37 // testing out setDatagramSocketImplFactory
38 System.err.println("setting DatagramSocketImplFactory...");
39 try {
40 DatagramSocket.setDatagramSocketImplFactory(new java.net.MyDatagramSocketImplFactory());
41 } catch (Exception ex) {
42 throw new RuntimeException("Setting DatagramSocketImplFactory failed!");
43 }
44 new QuoteServerThread().start();
45
46 // get a datagram socket
47 DatagramSocket socket = new DatagramSocket();
48
49 // send request
50 byte[] buf = new byte[256];
51 InetAddress address = InetAddress.getLocalHost();
52 DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
53 socket.send(packet);
54
55 // get response
56 packet = new DatagramPacket(buf, buf.length);
57 socket.receive(packet);
58
59 // display response
60 String received = new String(packet.getData(), 0);
61 System.err.println("Success!! Server current time is: " + received);
62
63 socket.close();
64 }
65}
66
67class QuoteServerThread extends Thread {
68
69 protected DatagramSocket socket = null;
70
71 public QuoteServerThread() throws IOException {
72 this("QuoteServerThread");
73 }
74
75 public QuoteServerThread(String name) throws IOException {
76 super(name);
77 socket = new DatagramSocket(4445);
78 }
79
80 public void run() {
81 try {
82 byte[] buf = new byte[256];
83
84 // receive request
85 DatagramPacket packet = new DatagramPacket(buf, buf.length);
86 socket.receive(packet);
87
88 // figure out response
89 String dString = null;
90 dString = new Date().toString();
91 buf = dString.getBytes();
92
93 // send the response to the client at "address" and "port"
94 InetAddress address = packet.getAddress();
95 int port = packet.getPort();
96 packet = new DatagramPacket(buf, buf.length, address, port);
97 socket.send(packet);
98 } catch (IOException e) {
99 e.printStackTrace();
100 }
101 socket.close();
102 }
103}