查詢客戶端 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 地址。這將顯示在控制檯上。

更新於: 2020 年 7 月 8 日

1K+ 檢視

啟動您的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.