Skip to content

Commit

Permalink
[minor] fix NetworkUtils#getHostname (apache#4355)
Browse files Browse the repository at this point in the history
(cherry picked from commit 478f9f3)
  • Loading branch information
danny0405 committed Dec 19, 2021
1 parent 3945258 commit 45769dd
Showing 1 changed file with 4 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,22 @@
import org.apache.hudi.exception.HoodieException;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
* A utility class for network.
*/
public class NetworkUtils {

public static synchronized String getHostname() {
Socket s = null;
try {
s = new Socket();
try (DatagramSocket s = new DatagramSocket()) {
// see https://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java
// for details.
s.connect(new InetSocketAddress("google.com", 80));
s.connect(InetAddress.getByName("8.8.8.8"), 10002);
return s.getLocalAddress().getHostAddress();
} catch (IOException e) {
throw new HoodieException("Unable to find server port", e);
} finally {
if (null != s) {
try {
s.close();
} catch (IOException e) {
throw new HoodieException("Unable to close server port", e);
}
}
}
}
}

0 comments on commit 45769dd

Please sign in to comment.