`

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

阅读更多
域名解析,将域名可转换为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.209
localhost:ip address 59.64.158.214
localhost:主机名: bupt
COM1
COM2


还有一个域名可能对应不止一个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.208
www.sina.com[1]: 121.194.0.209
www.sina.com[2]: 121.194.0.210
www.sina.com[3]: 121.194.0.203
www.sina.com[4]: 121.194.0.205
www.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…"


	}

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics