62,623
社区成员
发帖
与我相关
我的任务
分享
import java.net.*;
import java.io.*;
public class whois {
public final static int port = 43;
public final static String hostname = "whois.internic.net";
public void doSomething(String[] args){
Socket theSocket;
DataInputStream theWhoisStream;
PrintStream ps;
// 检查命令行参数
if (args.length < 1) {
System.out.println("\nUsage: java whois <command>");
System.out.println("Parameters:");
System.out
.println("\tcommand = one or more Domain name, or other command.");
System.out.println("Example:");
System.out.println("\tjava whois sohu.com");
System.out.println("\tjava whois help");
System.exit(1); // 退出
}
try {
// 在TCP服务端口43(十进制)连接SRI-NIC服务主机
theSocket = new Socket(hostname, port, true);
ps = new PrintStream(theSocket.getOutputStream());
// 发送用户提供的一个或多个命令
for (int i = 0; i < args.length; i++)
ps.print(args[i] + " ");
// 以回车和换行( <CRLF>)结尾
ps.print("\r\n");
// 接受相应命令的返回信息
theWhoisStream = new DataInputStream(theSocket.getInputStream());
String s;
BufferedWriter bw = new BufferedWriter(new FileWriter(
"D:/story/ip.txt",true)); // 附加方式 append
while ((s = theWhoisStream.readLine()) != null) {
System.out.println(s);
bw.write(s);
bw.newLine();
}
bw.flush();
bw.close();
// 关闭DataInputStream和PrintWriter
theWhoisStream.close();
ps.close();
// 关闭socket
theSocket.close();
} catch (IOException e) {
System.err.println(e);
}
}
public static void main(String[] args) {
whois test = new whois();
for(String s:args){
test.doSomething(new String[]{s});
}
}
}
import java.net.*;
import java.io.*;
public class whois {
public final static int port = 43;
public final static String hostname = "whois.internic.net";
public static void main(String[] args) {
Socket theSocket;
DataInputStream theWhoisStream;
PrintStream ps;
File f;
FileOutputStream fos;
BufferedWriter bw;
// 检查命令行参数
if (args.length < 1) {
System.out.println("\nUsage: java whois <command>");
System.out.println("Parameters:");
System.out
.println("\tcommand = one or more Domain name, or other command.");
System.out.println("Example:");
System.out.println("\tjava whois sohu.com");
System.out.println("\tjava whois help");
System.exit(1); // 退出
}
try {
// 在TCP服务端口43(十进制)连接SRI-NIC服务主机
theSocket = new Socket(hostname, port, true);
ps = new PrintStream(theSocket.getOutputStream());
// 发送用户提供的一个或多个命令
for (int i = 0; i < args.length; i++)
ps.print(args[i] + " ");
// 以回车和换行( <CRLF>)结尾
ps.print("\r\n");
// 接受相应命令的返回信息
theWhoisStream = new DataInputStream(theSocket.getInputStream());
String s;
f=new File("f:\\temp.txt");
fos =new FileOutputStream(f);
bw=new BufferedWriter(new OutputStreamWriter(fos));
while ((s = theWhoisStream.readLine()) != null) {
//System.out.println(s);
bw.write(s);
}
bw.flush();
bw.close();
// 关闭DataInputStream和PrintWriter
theWhoisStream.close();
ps.close();
// 关闭socket
theSocket.close();
} catch (IOException e) {
System.err.println(e);
}
}
}
.....
BufferedWriter bw = new BufferedWriter(new FileWriter("C:/whois_text.txt"));
while ((s = theWhoisStream.readLine()) != null) {
System.out.println(s);
bw.write(s);
bw.newLine();
}
bw.flush();
bw.close();
....