您的位置:首页 > 编程语言 > Java开发

java之InetAddress的使用

2011-06-16 22:23 357 查看
当今社会中,网络已经成了人们工作,娱乐,生活,休闲的一部分,网络程序设计就是开发为用户提供网络服务的实用程序,例如网络通信,股票行情,新闻资讯等。另外网络程序设计也是游戏开发的必修课

主要要用到下面几个方法:

InetAddress getLocalHost();

InetAddress getByName(String host);

String getHostName();

String getHostAddress();

boolean isReachable(int timeout);

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressDemo
{
public static void main(String[] args)
{
InetAddress localhost=null;
InetAddress remote=null;
try
{
localhost=InetAddress.getLocalHost();
System.out.println(localhost.getHostName());
System.out.println(localhost.getHostAddress());
System.out.println(localhost.isReachable(5000));
remote=InetAddress.getByName("www.google.com");
System.out.println(remote.getHostAddress());
System.out.println(remote.getHostName());
System.out.println(remote.isReachable(5000));
}
catch(UnknownHostException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}


运行结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: