InetAddressTest

来源:互联网 发布:济南用友软件代理商 编辑:程序博客网 时间:2024/06/11 08:37
package test.socket;import java.net.InetAddress;import java.net.UnknownHostException;/** * @author Administrator * Internet 地址获取 * */public class InetAddressTest {public static void main(String[] args) {if(args.length>0){String host=args[0];try {InetAddress[] inetAddresss=InetAddress.getAllByName(host);for(InetAddress a:inetAddresss){System.out.println("远端地址:"+a);}} catch (UnknownHostException e) {e.printStackTrace();}}else{try {InetAddress inetAddress=InetAddress.getLocalHost();System.out.println("主机名/主机地址:"+inetAddress);System.out.println("本机地址:"+inetAddress.getHostAddress());System.out.println("主机名:"+inetAddress.getHostName());} catch (UnknownHostException e) {e.printStackTrace();}}}}

0 0