During construction of a service requesting an ephemeral port
ensure the services' internal SocketAddress reflects the actual
port allocated by the operating system.
@Override
public void serverOpen() throws SocketException {
_datagramSocket = new DatagramSocket(_socketAddress.getPort(), _socketAddress.getAddress());
- _datagramSocket.setSoTimeout(100); // 1/10th second blocking timeout on receive()
+ _datagramSocket.setSoTimeout(100); // 1/10th second blocking timeout on receive()
+
+ if (_socketAddress.getPort() == Network.PORTS_EPHEMERAL) {
+ // reflect the actual port in use if an ephermal port was requested
+ InetSocketAddress actualSA = (InetSocketAddress)_datagramSocket.getLocalSocketAddress();
+ _socketAddress.setAddress(actualSA.getAddress());
+ _socketAddress.setPort(actualSA.getPort());
+ }
}
/**