📄 ch4.txt
字号:
/* 代码4-1
* Created on 2005-5-14
*/
import java.net.*;
public class GetByName {
public static void main (String[] args) {
try {
//通过域名获得IP地址
InetAddress address = InetAddress.getByName("java.sun.com");
System.out.println(address);
}
catch (UnknownHostException e) {
System.out.println("Could not find java.sun.com");
}
}
}
//PooledWeblog.java
import java.io.*;
import java.util.*;
import com.macfaq.io.SafeBufferedReader;
public class PooledWeblog {
private BufferedReader in;//输入流
private BufferedWriter out;//输出流
private int numberOfThreads;//记录线程的数量
private List entries = Collections.synchronizedList(new LinkedList( ));
private boolean finished = false;//标志是否结束
private int test = 0;
public PooledWeblog(InputStream in, OutputStream out, int numberOfThreads) {
this.in = new BufferedReader(new InputStreamReader(in));
this.out = new BufferedWriter(new OutputStreamWriter(out));
this.numberOfThreads = numberOfThreads;
}
public boolean isFinished( ) {
//当结束时返回
return this.finished;
}
public int getNumberOfThreads( ) {
return numberOfThreads;//返回线程的个数
}
public void processLogFile( ) {
for (int i = 0; i < numberOfThreads; i++) {
//去创建新的线程
Thread t = new LookupThread(entries, this);
t.start( );//启动线程
}
try {
String entry = null;
while ((entry = in.readLine( )) != null) {
if (entries.size( ) > numberOfThreads) {
try {
Thread.sleep((long) (1000.0/numberOfThreads));//停止线程一段时间
}
catch (InterruptedException e) {}
continue;
}
synchronized (entries) {
entries.add(0, entry);
entries.notifyAll( );
}
Thread.yield( );
} // 循环终止
}
catch (IOException e) {
System.out.println("Exception: " + e);//输出错误信息
}
this.finished = true;
// 结束那些仍然等待的线程
synchronized (entries) {
entries.notifyAll( );
}
}
//记录错误信息
public void log(String entry) throws IOException {
out.write(entry + System.getProperty("line.separator", "\r\n"));
out.flush( );
}
public static void main(String[] args) {
try {
PooledWeblog tw = new PooledWeblog(new FileInputStream(args[0]),
System.out, 100);
tw.processLogFile( );
}
catch (FileNotFoundException e) {
System.err.println("Usage: java PooledWeblog logfile_name");
}
catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Usage: java PooledWeblog logfile_name");
}
catch (Exception e) {
System.err.println(e);
e.printStackTrace( );
}
}
}
//LookupThread.java
import java.net.*;
import java.io.*;
import java.util.*;
public class LookupThread extends Thread {
private List entries;
PooledWeblog log; //在回调的时候使用
//检索线程是否在列表中
public LookupThread(List entries, PooledWeblog log) {
this.entries = entries;
this.log = log;
}
//启动线程的函数
public void run( ) {
String entry;
while (true) {
synchronized (entries) {
while (entries.size( ) == 0) {
if (log.isFinished( )) return;
try {
entries.wait( );
}
catch (InterruptedException e) {
}
}
entry = (String) entries.remove(entries.size( )-1);
}
int index = entry.indexOf(' ', 0);
//获得远程主机地址
String remoteHost = entry.substring(0, index);
String theRest = entry.substring(index, entry.length( ));
try {
//获得远程主机的地址
remoteHost =InetAddress.getByName(remoteHost).getHostName( );
}
catch (Exception e) {
}
try {
log.log(remoteHost + theRest);
}
catch (IOException e) {
}
this.yield( );
}
}
}
/* 代码4-2
* Created on 2005-5-14
*/
import java.net.*;
public class GetByAddress {
public static void main (String[] args) {
try {
//通过IP地址作为参数
InetAddress address = InetAddress.getByName("209.249.116.143");
System.out.println(address);//打印出此地址
}
catch (UnknownHostException e) {
System.out.println("Could not find 209.249.116.143");//打印出错误地址
}
}
}
import java.net.*;
public class AllAddressesOfMicrosoft {
public static void main (String[] args) {
try {
//获得地址组
InetAddress[] addresses =
InetAddress.getAllByName("www.microsoft.com");//通过域名获得一个地址组
for (int i = 0; i < addresses.length; i++) {
System.out.println(addresses[i]);
}
}
catch (UnknownHostException e) {
System.out.println("Could not find www.microsoft.com");//打印错误信息
}
}
}
import java.net.*;
public class MyAddress {
public static void main (String[] args) {
try {
//获取本地IP地址
InetAddress address = InetAddress.getLocalHost( );
System.out.println(address);//打印IP地址
}
catch (UnknownHostException e) {
System.out.println("Could not find this computer's address.");//打印错误信息
}
}
}
/* 代码4-3
* Created on 2005-5-14
*/
package ch5;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class HostResolver {
void showHelp() {
// 打印帮助信息
System.out.println("Usage:\r\njava HostResolver hostname");
}
HostResolver(String[] args) {
if (1 != args.length) {
showHelp();
System.exit(0);
}
try {
// 通过getByName()静态方法构造InetAddress的实例
InetAddress addr = InetAddress.getByName(args[0]);
// 下面测试InetAddress类所代表的IP地址的属性
// 返回此地址对应的主机名
System.out.println("Hostname: " + addr.getHostName());
// 返回此地址对象对应的IP地址
System.out.println("IP Address: " + addr.getHostAddress());
// 返回此IP地址的原始表达
System.out.println("Raw Address : ");
// 测试这个地址是不是一个通配地址
System.out.println("isAnyLocalAddress : " + addr.isAnyLocalAddress());
// 测试这个地址是不是环回地址
System.out.println("isLoopbackAddress : " + addr.isLoopbackAddress());
// 测试这个地址是不是一个多播地址
System.out.println("isMulticastAddress : " + addr.isMulticastAddress());
} catch (UnknownHostException e) {
// 出现异常,无法解析主机地址
System.err.println("Unkonw hostname : " + args[0]);
}
}
public static void main(String[] args) {
HostResolver hr = new HostResolver(args);
}
}
/* 代码4-4
* Created on 2005-5-14
*/
//Weblog.java
import java.net.*;
import java.io.*;
import java.util.*;
import com.macfaq.io.SafeBufferedReader;
public class Weblog {
public static void main(String[] args) {
Date start = new Date( );//得到起始日期
try {
//使用FileInputStream 类来装饰输入流
FileInputStream fin = new FileInputStream(args[0]);
Reader in = new InputStreamReader(fin);//获得Read类型输入流
SafeBufferedReader bin = new SafeBufferedReader(in);
String entry = null;
while ((entry = bin.readLine( )) != null) {
// 分离出IP地址
int index = entry.indexOf(' ', 0);
String ip = entry.substring(0, index);//得到IP地址
String theRest = entry.substring(index, entry.length( ));
// 找到主机名字,并打印出来
try {
InetAddress address = InetAddress.getByName(ip);//通过IP地址获得此地址
System.out.println(address.getHostName( ) + theRest);
}
catch (UnknownHostException e) {
System.out.println(entry);
}
} // 循环终止
}
catch (IOException e) {
System.out.println("Exception: " + e);
}
Date end = new Date( );//得到新日期
long elapsedTime = (end.getTime()-start.getTime( ))/1000;
System.out.println("Elapsed time: " + elapsedTime + " seconds");
} // main终止
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -