(网络编程)InetAddress(表示ip地址的类)相关使用
生活随笔
收集整理的這篇文章主要介紹了
(网络编程)InetAddress(表示ip地址的类)相关使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import java.net.InetAddress;
import java.net.UnknownHostException;
//測試ip
public class TestInetAddress {public static void main(String[] args) {try {//查詢本機(jī)ip地址System.out.println("查詢本機(jī)ip地址:");InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");System.out.println(inetAddress1);InetAddress inetAddress2 = InetAddress.getByName("localhost");System.out.println(inetAddress2);InetAddress inetAddress3 = InetAddress.getLocalHost();System.out.println(inetAddress3);System.out.println("-----------------------------------");//查詢網(wǎng)絡(luò)ip地址System.out.println("查詢網(wǎng)絡(luò)ip地址:");InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");System.out.println(inetAddress4);System.out.println("-----------------------------------");//常用方法System.out.println("常用方法:");System.out.println(inetAddress4.getCanonicalHostName());//規(guī)范的名字System.out.println(inetAddress4.getHostAddress());System.out.println(inetAddress4.getHostName());} catch (UnknownHostException e) {System.out.println("111");}}
}
總結(jié)
以上是生活随笔為你收集整理的(网络编程)InetAddress(表示ip地址的类)相关使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (JAVA学习笔记) 异常处理
- 下一篇: (网络编程)TCP实现聊天