changeset 13441:f746a5e4a0f6

8133704: java/net/DatagramSocket/SetDatagramSocketImplFactory/ADatagramSocket.java may fail with address already in use Reviewed-by: chegar Contributed-by: felix.yang@oracle.com
author xiaofeya
date Mon, 11 Jan 2016 00:21:50 -0800
parents aa9fd2797b82
children 8630d3de3737
files test/java/net/DatagramSocket/SetDatagramSocketImplFactory/ADatagramSocket.java
diffstat 1 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/net/DatagramSocket/SetDatagramSocketImplFactory/ADatagramSocket.java	Sun Jan 10 11:09:31 2016 -0800
+++ b/test/java/net/DatagramSocket/SetDatagramSocketImplFactory/ADatagramSocket.java	Mon Jan 11 00:21:50 2016 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -41,7 +41,11 @@
         } catch (Exception ex) {
           throw new RuntimeException("Setting DatagramSocketImplFactory failed!");
         }
-        new QuoteServerThread().start();
+
+        QuoteServerThread server = new QuoteServerThread();
+        int port = server.getPort();
+        System.out.println("Server port is " + port);
+        server.start();
 
         // get a datagram socket
         DatagramSocket socket = new DatagramSocket();
@@ -49,7 +53,7 @@
         // send request
         byte[] buf = new byte[256];
         InetAddress address = InetAddress.getLocalHost();
-        DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
+        DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port);
         socket.send(packet);
 
         // get response
@@ -67,6 +71,7 @@
 class QuoteServerThread extends Thread {
 
     protected DatagramSocket socket = null;
+    private final int port;
 
     public QuoteServerThread() throws IOException {
         this("QuoteServerThread");
@@ -74,7 +79,11 @@
 
     public QuoteServerThread(String name) throws IOException {
         super(name);
-        socket = new DatagramSocket(4445);
+        socket = new DatagramSocket(0);
+        port =  socket.getLocalPort();
+    }
+    public int getPort(){
+        return port;
     }
 
     public void run() {
@@ -101,3 +110,4 @@
       socket.close();
     }
 }
+