您的位置:首页 > 理论基础 > 计算机网络

java网络编程根据域名获取ip地址,URL

2014-09-18 16:22 567 查看
域名解析,将域名可转换为ip地址InetAddress也可以通过使用getAddress()来获得IP地址,但是它的返回值是一个4个字节的数组。因此尽管getAddress()在获得IP方面是有用的,但却不适于用来输出。
package dns;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
public class NsLookup {  public static void main(String[] args){
InetAddress address=null;
try { address = InetAddress.getByName(args[0]);
} catch (UnknownHostException e) {
e.printStackTrace();
} System.out.println(args[0]+": "+address.getHostAddress()); //args[0]是执行程序时写的参数,
InetAddress localhost=null;
try { localhost = InetAddress.getLocalHost(); //本地地址
} catch (UnknownHostException e) {
e.printStackTrace();
} System.out.println("localhost:ip address "+localhost.getHostAddress());
System.out.println("localhost:主机名: "+localhost.getHostName());
/* *  在开始使用RS232端口通讯之前,我们想知道系统有哪些端口是可用的,以下代码列出系统中所有可用的RS232端口
*/ CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
// iterate through the ports.
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
} } } }
执行时命令行参数如果输入:www.sina.com执行结果如下:www.sina.com: 121.194.0.209localhost:ip address 59.64.158.214localhost:主机名: buptCOM1COM2还有一个域名可能对应不止一个ip地址,一下程序时列举出sina域名下的所有ip
package dns;
import java.net.InetAddress;
import java.net.UnknownHostException;
//有时一个域名会包含不止一个IP地址,比如微软的Web服务器,这是为了保持负载平衡。
//InetAddress提供了一种可以得到一个域名的所有IP地址的方法
public class NsLookup2 {
static public void main(String[] args) {
try { String name = args[0];
InetAddress[] addresses = InetAddress.getAllByName(name);
for (int i = 0; i < addresses.length; i++) {
System.out.println(name + "[" + i + "]: "
+ addresses[i].getHostAddress());
} } catch (UnknownHostException uhe) {
System.err.println("Unable to find: " + args[0]);
} } }
执行结果: www.sina.com[0]: 121.194.0.208www.sina.com[1]: 121.194.0.209www.sina.com[2]: 121.194.0.210www.sina.com[3]: 121.194.0.203www.sina.com[4]: 121.194.0.205www.sina.com[5]: 121.194.0.206下面是一个根据url可以输出其页面html源码的java代码:
package urldemo;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.io.PrintStream;
public class URLReader {
/** * @param args
*/ public static void main(String[] args) throws Exception {
URL tirc = new URL("http://www.sina.com.cn/");// 构建一URL对象
BufferedReader in = new BufferedReader(new InputStreamReader(tirc
.openStream()));
// 使用openStream得到一输入流并由此构造一个BufferedReader对象
String inputLine;
while ((inputLine = in.readLine()) != null)
// 从输入流不断的读数据,直到读完为止
System.out.println(inputLine); // 把读入的数据打印到屏幕上
in.close(); // 关闭输入流
//  URL url =new URL ("http://www.sina.com.cn/");//创建一URL对象
//  URLConnection con=url.openConnection();  //由URL对象获取URLConnection对象
//  DataInputStream dis=new DataInputStream (con.getInputStream()); //由URLConnection获取输入流,并构造DataInputStream对象
//  PrintStream ps=new PrintStream(con.getOutputStream());  //由URLConnection获取输出流,并构造PrintStream对象
//  String line=dis.readLine(); //从服务器读入一行
//  ps.println("client…"); //向服务器写出字符串 "client…"
} }
http://it.chinawin.net/softwaredev/article-17f51.html
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: