Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- /**
- *
- * @author Julio Chinchilla
- */
- public class Inet {
- /**
- * Devuelve la ip de un host determinado, ejem:
- * getHostIP("google.com", 80);
- * @param host dirección url del host
- * @param port puerto de conexión
- * @return String IP del host y puerto en conexión con el Socket
- * @throws IOException
- */
- public static String getHostIP(String host, int port) throws IOException {
- Socket s = new Socket(host, port);
- String ip = s.getLocalAddress().getHostAddress();
- s.close();
- return ip;
- }
- /**
- * Devuelve la ip local de la máquina
- * @return String IP del host donde se ejecutó
- * @throws UnknownHostException
- */
- public static String getLocalIP () throws UnknownHostException {
- InetAddress ip = InetAddress.getLocalHost();
- return ip.getHostAddress();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement