Advertisement
juliomzt

getIP

Jun 1st, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.InetAddress;
  3. import java.net.Socket;
  4. import java.net.UnknownHostException;
  5.  
  6. /**
  7.  *
  8.  * @author Julio Chinchilla
  9.  */
  10. public class Inet {
  11.    
  12.     /**
  13.      * Devuelve la ip de un host determinado, ejem:
  14.      * getHostIP("google.com", 80);
  15.      * @param host dirección url del host
  16.      * @param port puerto de conexión
  17.      * @return String IP del host y puerto en conexión con el Socket
  18.      * @throws IOException
  19.      */
  20.     public static String getHostIP(String host, int port) throws IOException {
  21.         Socket s = new Socket(host, port);
  22.         String ip = s.getLocalAddress().getHostAddress();
  23.         s.close();
  24.         return ip;
  25.     }
  26.    
  27.     /**
  28.      * Devuelve la ip local de la máquina
  29.      * @return String IP del host donde se ejecutó
  30.      * @throws UnknownHostException
  31.      */
  32.     public static String getLocalIP () throws UnknownHostException {
  33.         InetAddress ip = InetAddress.getLocalHost();
  34.     return ip.getHostAddress();
  35.     }
  36.    
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement