在網路世界裡,每台計算機都有唯一的IP地址,這個地址能夠標識計算機的位置。我們可以以當前計算機的IP地址為中心,進行一些有趣的探索。
一、IP地址的獲取
IP地址是指互聯網協議地址,是用於在網路中識別設備的邏輯地址。我們可以通過以下的Java代碼獲取當前機器的IP地址。
import java.net.InetAddress;
public class GetIp{
public static void main(String[] args) throws Exception{
InetAddress address = InetAddress.getLocalHost();
System.out.println("IP地址:"+address.getHostAddress());
}
}
代碼中,我們調用了Java提供的InetAddress類中的getLocalHost()方法獲取本機地址。通過調用getHostAddress()方法獲取IP地址。這段代碼將會輸出當前IP地址。
二、根據IP地址獲取地理位置
除了獲得IP地址以外,我們還可以通過一些API,將IP地址轉換成地理位置信息。下面是一個根據IP地址獲取地理位置的Java示例代碼:
import net.sf.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class GetAddressByIP {
public static void main(String[] args) {
String ip = "114.114.114.114";
String urlStr = "http://ip-api.com/json/" + ip + "?lang=zh-CN";
String address = "";
try {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuffer stringBuffer = new StringBuffer();
String temp = "";
while ((temp = bufferedReader.readLine()) != null) {
stringBuffer.append(temp);
}
JSONObject jsonObject = JSONObject.fromObject(stringBuffer.toString());
address = jsonObject.getString("city") + ", " + jsonObject.getString("regionName");
System.out.println(address);
} catch (Exception e) {
e.printStackTrace();
}
}
}
代碼中,我們調用了百度地圖API的服務,將IP地址轉換成了地理位置信息。通過訪問http://ip-api.com/json/ IP地址 ?lang=zh-CN 獲得JSON格式的數據,通過解析數據,我們可以獲取城市和地區信息。
三、掃描區域網中的其他主機
在同一個網路下,我們可以通過掃描區域網中的其他主機來了解當前網路中開啟的計算機。下面是一個簡單的Java代碼片段,它可以掃描同一子網中的其他主機。
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
public class ScanIP {
public static void main(String[] args) {
Enumeration netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = netInterfaces.nextElement();
Enumeration inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
String ip = inetAddress.getHostAddress();
int i = ip.lastIndexOf(".");
String add = ip.substring(0, i + 1);
for (int j = 1; j < 255; j++) {
String testIp = add + j;
InetAddress address = InetAddress.getByName(testIp);
boolean reachable = address.isReachable(3000);
if (reachable) {
System.out.println(testIp + " is reachable");
} else {
System.out.println(testIp + " is not reachable");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
代碼中,我們獲取本機所有的網路介面,並對每個介面遍歷其子網中的所有IP地址,通過訪問IP地址的方式判斷該地址是否在使用中。
四、根據IP地址進行埠掃描
在同一個網路下,我們還可以通過埠掃描的方式了解當前網路中開放了哪些埠。下面是一個Java代碼示例,通過掃描指定IP的多個埠,獲取其埠開放情況。
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class PortScan{
public static void main(String[] args) {
String ip = "192.168.0.1";
for (int i = 1; i <= 65535; i++) {
final int port = i;
new Thread(new Runnable() {
@Override
public void run() {
Socket socket = new Socket();
try {
socket.connect(new InetSocketAddress(ip, port), 10);
System.out.println(ip + " : " + port + " is open.");
socket.close();
} catch (IOException e) {
//System.out.println(ip + " : " + port + " is closed.");
}
}
}).start();
}
}
}
代碼中,我們通過掃描IP地址下的所有埠(1~65535),判斷其是否開放。開放的埠將會輸出相關信息,而未開放的埠將不做處理。
五、IP地址中數字的含義
IP地址是由四個0~255的數字組成,例如:192.168.1.1。這些數字具有一定的含義。其中,前3個數字組成的是網路地址,而最後一個數字則是主機地址。下面是一個Java示例代碼,用於解析IP地址中的數字含義。
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IpAnalyze {
public static void main(String[] args) throws UnknownHostException {
String ipAddress = "192.168.1.100";
InetAddress inetAddress = InetAddress.getByName(ipAddress);
byte[] byteAddress = inetAddress.getAddress();
int[] intAddress = new int[4];
for (int i = 0; i < 4; i++) {
intAddress[i] = byteAddress[i] < 0 ? byteAddress[i] + 256 : byteAddress[i];
}
System.out.println("IP地址:" + ipAddress);
System.out.println("網路地址:" + intAddress[0] + "." + intAddress[1] + "." + intAddress[2]);
System.out.println("主機地址:" + intAddress[3]);
}
}
代碼中,我們通過調用Java的InetAddress類,將IP地址轉換成一個InetAddress對象。通過getAddress()方法獲取IP地址的位元組形式,然後將其轉換為int類型,就可以獲得網路地址和主機地址了。
六、總結
本文介紹了以本機當前IP地址為中心的探索,從IP地址的獲取、根據IP地址獲取地理位置、掃描區域網中的其他主機、根據IP地址進行埠掃描、IP地址中數字的含義等多個方面展示了與IP地址相關的Java代碼示例。通過這些Java代碼示例,我們可以更深入地了解IP地址,使用Java實現更多有趣的網路應用。
原創文章,作者:JRCJ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/140282.html