pekko/akka-docs/rst/java/code/docs/util/SocketUtils.java
2016-01-14 00:31:03 +01:00

31 lines
No EOL
798 B
Java

/**
* Copyright (C) 2015 Typesafe Inc. <http://www.typesafe.com>
*/
package docs.util;
import java.net.InetSocketAddress;
import java.io.IOException;
import java.net.ServerSocket;
import java.nio.channels.ServerSocketChannel;
public class SocketUtils {
public static InetSocketAddress temporaryServerAddress(String hostname) {
try {
ServerSocket socket = ServerSocketChannel.open().socket();
socket.bind(new InetSocketAddress(hostname, 0));
InetSocketAddress address = new InetSocketAddress(hostname, socket.getLocalPort());
socket.close();
return address;
}
catch (IOException io) {
throw new RuntimeException(io);
}
}
public static InetSocketAddress temporaryServerAddress() {
return temporaryServerAddress("127.0.0.1");
}
}