Here you can find the source of getIpByName(String name)
Parameter | Description |
---|---|
name | nombre a resolver |
static public String getIpByName(String name)
//package com.java2s; /*//from w w w . ja v a 2 s . c om * ISABEL: A group collaboration tool for the Internet * Copyright (C) 2009 Agora System S.A. * * This file is part of Isabel. * * Isabel is free software: you can redistribute it and/or modify * it under the terms of the Affero GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Isabel is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * Affero GNU General Public License for more details. * * You should have received a copy of the Affero GNU General Public License * along with Isabel. If not, see <http://www.gnu.org/licenses/>. */ import java.net.*; public class Main { /** * Calcula la ip a partir del nombre * @param name nombre a resolver * @return ip * @Author enrique */ static public String getIpByName(String name) { //Cambio la direccion por la direccion literal y la pongo //entre corchetes si es IPv6, si tiene varias me quedo con la ipv4 InetAddress[] address; try { address = InetAddress.getAllByName(name); } catch (UnknownHostException e) { e.printStackTrace(); return null; } int i = 0; String ip = ""; if (address.length > 1) { for (i = 0; i < address.length; i++) { if (address[i] instanceof Inet4Address) { ip = address[i].getHostAddress(); break; } } } else { ip = address[0].getHostAddress(); } if (address[i] instanceof Inet6Address) ip = "[" + ip + "]"; return ip; } }