1/* 2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. 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 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. 22 */ 23 24/* @test 25 * @bug 4132931 26 * @summary DatagramSocket should use a factory for its impl 27 * 28 * @compile/module=java.base java/net/MyDatagramSocketImplFactory.java 29 * @run main ADatagramSocket 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 45 QuoteServerThread server = new QuoteServerThread(); 46 int port = server.getPort(); 47 System.out.println("Server port is " + port); 48 server.start(); 49 50 // get a datagram socket 51 DatagramSocket socket = new DatagramSocket(); 52 53 // send request 54 byte[] buf = new byte[256]; 55 InetAddress address = InetAddress.getLocalHost(); 56 DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port); 57 socket.send(packet); 58 59 // get response 60 packet = new DatagramPacket(buf, buf.length); 61 socket.receive(packet); 62 63 // display response 64 String received = new String(packet.getData(), 0); 65 System.err.println("Success!! Server current time is: " + received); 66 67 socket.close(); 68 } 69} 70 71class QuoteServerThread extends Thread { 72 73 protected DatagramSocket socket = null; 74 private final int port; 75 76 public QuoteServerThread() throws IOException { 77 this("QuoteServerThread"); 78 } 79 80 public QuoteServerThread(String name) throws IOException { 81 super(name); 82 socket = new DatagramSocket(0); 83 port = socket.getLocalPort(); 84 } 85 public int getPort(){ 86 return port; 87 } 88 89 public void run() { 90 try { 91 byte[] buf = new byte[256]; 92 93 // receive request 94 DatagramPacket packet = new DatagramPacket(buf, buf.length); 95 socket.receive(packet); 96 97 // figure out response 98 String dString = null; 99 dString = new Date().toString(); 100 buf = dString.getBytes(); 101 102 // send the response to the client at "address" and "port" 103 InetAddress address = packet.getAddress(); 104 int port = packet.getPort(); 105 packet = new DatagramPacket(buf, buf.length, address, port); 106 socket.send(packet); 107 } catch (IOException e) { 108 e.printStackTrace(); 109 } 110 socket.close(); 111 } 112} 113 114