test/jdk/java/net/DatagramSocket/SetDatagramSocketImplFactory/ADatagramSocket.java
changeset 57686 70f5cbb711a9
parent 47216 71c04702a3d5
equal deleted inserted replaced
57685:e4cc5231ce2d 57686:70f5cbb711a9
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    29  * @run main ADatagramSocket
    29  * @run main ADatagramSocket
    30  */
    30  */
    31 import java.io.*;
    31 import java.io.*;
    32 import java.net.*;
    32 import java.net.*;
    33 import java.util.*;
    33 import java.util.*;
       
    34 import java.util.concurrent.CountDownLatch;
    34 
    35 
    35 public class ADatagramSocket {
    36 public class ADatagramSocket {
    36     public static void main(String[] args) throws IOException {
    37     public static void main(String[] args) throws Exception {
    37         // testing out setDatagramSocketImplFactory
    38         // testing out setDatagramSocketImplFactory
    38         System.err.println("setting DatagramSocketImplFactory...");
    39         System.err.println("setting DatagramSocketImplFactory...");
    39         try {
    40         try {
    40           DatagramSocket.setDatagramSocketImplFactory(new java.net.MyDatagramSocketImplFactory());
    41           DatagramSocket.setDatagramSocketImplFactory(new java.net.MyDatagramSocketImplFactory());
    41         } catch (Exception ex) {
    42         } catch (Exception ex) {
    44 
    45 
    45         QuoteServerThread server = new QuoteServerThread();
    46         QuoteServerThread server = new QuoteServerThread();
    46         int port = server.getPort();
    47         int port = server.getPort();
    47         System.out.println("Server port is " + port);
    48         System.out.println("Server port is " + port);
    48         server.start();
    49         server.start();
       
    50         // Wait server thread to reach receive call
       
    51         server.readyToStart.await();
    49 
    52 
    50         // get a datagram socket
    53         // get a datagram socket
    51         DatagramSocket socket = new DatagramSocket();
    54         DatagramSocket socket = new DatagramSocket();
    52 
    55 
    53         // send request
    56         // send request
    70 
    73 
    71 class QuoteServerThread extends Thread {
    74 class QuoteServerThread extends Thread {
    72 
    75 
    73     protected DatagramSocket socket = null;
    76     protected DatagramSocket socket = null;
    74     private final int port;
    77     private final int port;
       
    78     final CountDownLatch readyToStart = new CountDownLatch(1);
    75 
    79 
    76     public QuoteServerThread() throws IOException {
    80     public QuoteServerThread() throws IOException {
    77         this("QuoteServerThread");
    81         this("QuoteServerThread");
    78     }
    82     }
    79 
    83 
    80     public QuoteServerThread(String name) throws IOException {
    84     public QuoteServerThread(String name) throws IOException {
    81         super(name);
    85         super(name);
    82         socket = new DatagramSocket(0);
    86         socket = new DatagramSocket(0, InetAddress.getLocalHost());
    83         port =  socket.getLocalPort();
    87         port =  socket.getLocalPort();
    84     }
    88     }
    85     public int getPort(){
    89     public int getPort(){
    86         return port;
    90         return port;
    87     }
    91     }
    90       try {
    94       try {
    91         byte[] buf = new byte[256];
    95         byte[] buf = new byte[256];
    92 
    96 
    93         // receive request
    97         // receive request
    94         DatagramPacket packet = new DatagramPacket(buf, buf.length);
    98         DatagramPacket packet = new DatagramPacket(buf, buf.length);
       
    99         // Notify client that server is ready to receive packet
       
   100         readyToStart.countDown();
    95         socket.receive(packet);
   101         socket.receive(packet);
    96 
   102 
    97         // figure out response
   103         // figure out response
    98         String dString = null;
   104         String dString = null;
    99         dString = new Date().toString();
   105         dString = new Date().toString();