查找客户端 IP 地址的 Java 程序
要查找客户端的 IP 地址,Java 代码如下 -
示例
import java.net.*; import java.io.*; import java.util.*; import java.net.InetAddress; public class Demo{ public static void main(String args[]) throws Exception{ InetAddress my_localhost = InetAddress.getLocalHost(); System.out.println("The IP Address of client is : " + (my_localhost.getHostAddress()).trim()); String my_system_address = ""; try{ URL my_url = new URL("http://bot.whatismyipaddress.com"); BufferedReader my_br = new BufferedReader(new InputStreamReader(my_url.openStream())); my_system_address = my_br.readLine().trim(); } catch (Exception e){ my_system_address = "Cannot Execute Properly"; } } }
输出
The IP Address of client is : 127.0.0.1
一个名为 Demo 的类包含主函数。在此主函数中,创建了 InetAddress 类的实例,并使用“getHostAddress”函数获取客户端的 IP 地址。这将显示在控制台上。
广告