ในการค้นหาที่อยู่ 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 ของไคลเอ็นต์ สิ่งนี้จะแสดงบนคอนโซล