Để tìm địa chỉ IP của client, mã Java như sau:
Ví dụ
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("https://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"; } } }
Kết quả
The IP Address of client is : 127.0.0.1
Lớp Demo chứa hàm main. Trong main, thể hiện InetAddress tạo và hàm getHostAddress dùng lấy IP client. Hiển thị trên console.