net url address hostname
5013 ワード
キーワードはドメイン名を知ってIPを求めてIPを知ってドメイン名を求めます
public class Client extends TestCase {
public void test1() {
try {
InetAddress[] a = InetAddress.getAllByName("localhost");
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
// byte
String str = "61.135.253.16";
String[] ipStr = str.split("\\.");// "."
byte[] ipBuf = new byte[4];
for (int i = 0; i < 4; i++) {
ipBuf[i] = (byte) (Integer.parseInt(ipStr[i]) & 0xFF);// 。
// byte -128~127
}
InetAddress ia = InetAddress.getByAddress(ipBuf);
System.out.println(ia.getHostName());// hostname
ia = InetAddress.getByName("129.42.60.216");
System.out.println(ia.getHostName() + " " + ia);// hostname
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
// ip
public static String getHostName() {
String s = "";
try {
String s1 = "ipconfig /all";
Process process = Runtime.getRuntime().exec(s1);
BufferedReader bufferedreader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String nextLine;
for (String line = bufferedreader.readLine(); line != null; line = nextLine) {
nextLine = bufferedreader.readLine();
if (line.indexOf("Host Name") <= 0) {
continue;
}
int i = line.indexOf("Host Name") + 36;
s = line.substring(i);
break;
}
bufferedreader.close();
process.waitFor();
} catch (Exception exception) {
s = "";
}
return s.trim();
}
// ip
public static String getHostNameByNbtstat(String clientIP) {
String s = "";
String sb = clientIP.trim().substring(0, 3);
//System.out.println("clientIP="+clientIP+"\t"+" :"+sb);
if(sb.equals("127")){
//System.out.println(" 127.0.0.1");
s = getHostName();
}
else{
//System.out.println(" ip");
try {
String s1 = "nbtstat -a "+clientIP;
Process process = Runtime.getRuntime().exec(s1);
BufferedReader bufferedreader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String nextLine;
int y = 0;
int x = 0;
for (String line = bufferedreader.readLine(); line != null; line = nextLine) {
nextLine = bufferedreader.readLine();
//System.out.println("y= "+y+" nextLine="+nextLine);
if(y==13){
//System.out.println(" :-----------------");
//System.out.println(nextLine.indexOf("<00>")+"--------");
s = (nextLine.substring(0, (nextLine.indexOf("<00>")))).toLowerCase();//
}
y++;
}
bufferedreader.close();
process.waitFor();
} catch (Exception exception) {
s = "";
}
}
return s.trim();
}
public static void main(String[] args) {
String name = getHostNameByNbtstat("61.135.253.16");//server window
System.out.println("name="+name);
}
}