InetAddress实例

作者在 2010-05-24 09:16:49 发布以下内容
import java.net.InetAddress;

public class TestInetAddress {
    public static void main( String args[]) {
        try{
            InetAddress ia = InetAddress.getLocalHost();
            showInfo(ia);
            
            ia = InetAddress.getByName("www.sina.com.cn");
            showInfo(ia);
        }catch(java.net.UnknownHostException e){
            e.printStackTrace();
        }
        
    }
    
    public static void showInfo(InetAddress ia){
        String name = ia.getHostName();//得到对方的域名或机器名称
        String address = ia.getHostAddress();//得到对方的IP地址
        System.out.println(name);
        System.out.println(address);
        System.out.println("----------------");
    }
}
网络编程 | 阅读 834 次
文章评论,共0条
游客请输入验证码
浏览290878次