Here you can find the source of getGlobalAddress(String url)
Parameter | Description |
---|---|
url | the url to find the global IP |
public static Inet4Address getGlobalAddress(String url)
//package com.java2s; /******************************************************************************* * Copyright (c) 2011 MadRobot.// ww w. j a v a 2 s . c o m * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * Contributors: * Elton Kent - initial API and implementation ******************************************************************************/ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Inet4Address; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class Main { /** * Finds this computer's global IP address * * @param url * the url to find the global IP * @return The global IP address, or null if a problem occurred */ public static Inet4Address getGlobalAddress(String url) { try { URLConnection uc = new URL(url).openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream())); return (Inet4Address) InetAddress.getByName(br.readLine()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }