[JAVA] Get network information
This is the source code to get network information.
It would be good for reference.
ref : Showing IP address of my device in the network? - Processing 2.x and 3.x Forum
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
private static final String DEFAULT_1 = "IPv4 주소 . . . . . . . . . :";
private static final String DEFAULT_2 = "서브넷 마스크 . . . . . . . :";
private static final String DEFAULT_3 = "기본 게이트웨이 . . . . . . :";
String INFO_IP = "";
String INFO_SUB = "";
String INFO_GW = "";
void setup() {
println(findLanIp());
// println(findLangatewayIp());
String lines="";
lines = findLangatewayIp();
println(lines);
if(lines == null) { return; }
int nums1=lines.indexOf(DEFAULT_1);
int nums2=lines.indexOf(DEFAULT_2);
if( (nums1 != -1)&&(nums2 != -1) ){
INFO_IP=lines.substring(nums1+DEFAULT_1.length()+1, nums2);
}
nums1=lines.indexOf(DEFAULT_2);
nums2=lines.indexOf(DEFAULT_3);
if( (nums1 != -1)&&(nums2 != -1) ){
INFO_SUB=lines.substring(nums1+DEFAULT_2.length()+1, nums2);
}
nums1=lines.indexOf(DEFAULT_3);
if( (nums1 != -1)&&(nums1 != -1) ){
INFO_GW=lines.substring(nums1+DEFAULT_3.length()+1, lines.length());
}
lines="";
println(" INFO_IP : " + INFO_IP);
println(" INFO_SUB : " + INFO_SUB);
println(" INFO_GW : " + INFO_GW);
exit();
}
//====================================================================//
// SIMPLE GET IP
static final String findLanIp() {
try {
return InetAddress.getLocalHost().getHostAddress();
}
catch (final UnknownHostException notFound) {
System.err.println("No LAN IP found!");
return "";
}
}
//====================================================================//
//====================================================================//
// IPCONFIG GET
private static final String DEFAULT_GATEWAY = "Default Gateway";
String findLangatewayIp() {
String line_r="";
if (Desktop.isDesktopSupported()) {
try {
Process process = Runtime.getRuntime().exec("ipconfig");
try {
String line;
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream()));
while ((line = bufferedReader.readLine()) != null) {
line_r = line_r + line;
// System.out.println(line);
}
}catch (Exception e) {}
} catch (Exception e) {
e.printStackTrace();
}}
return line_r;
}
//====================================================================//