zoukankan      html  css  js  c++  java
  • Java实现Socket之WhoisClient

    Java实现Socket之WhoisClient

    代码内容

    • 从常用的whois服务器的43号端口得到对应域名的所有注册信息并显示出来

    代码实现

    /* WhoisClient.java */
    import java.io.*;
    import java.net.*;
    
    public class WhoisClient {
    	public static void main(String[] args) {
    		BufferedReader systemIn = new BufferedReader(new InputStreamReader(System.in));
    		System.out.println("Enter the domain names. Enter "exit" to quit.");
    		try {
    			while (true) {
    				/* 输入流提示控制 */
    				System.out.print("> ");
    				String host = (args.length > 0) ? args[0] : systemIn.readLine();
    				if (host.isEmpty()) {
    					continue;
    				} else if (host.equalsIgnoreCase("exit")) {
    					break;
    				}
    				whois(host);
    				if (args.length > 0)
    					break;
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	private static void whois(String host) {
    		try {
    			/* 设置服务器地址和端口号 */
    			InetAddress server = InetAddress.getByName("whois.markmonitor.com");
    			int port = 43;
    			/* 建立Socket连接 */
    			Socket socket = new Socket(server, port);
    			/* 写入发送消息到Socket */
    			PrintWriter out = new PrintWriter(socket.getOutputStream());
    			out.println(host);
    			out.flush();
    			/* 从Socket读取消息 */
    			BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    			/* 格式化输出 */
    			StringBuilder ret = new StringBuilder();
    			String line;
    			while ((line = in.readLine()) != null) {
    				ret.append(line + "
    ");
    			}
    			/* 安全关闭Socket */
    			socket.close();
    			/* 结果输出判断 */
    			if (ret.toString().trim().isEmpty()) {
    				System.out.println("该域名不在whois上注册");
    			} else {
    				System.out.println(ret.toString());
    			}
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    运行截图




  • 相关阅读:
    业务逻辑层封装
    了解ASP.NET MVC几种ActionResult的本质:EmptyResult & ContentResult
    Facade外观模式
    了解ASP.NET MVC几种ActionResult的本质:FileResult
    示例代码浏览器5.4功能更新
    面试体验:Microsoft 篇
    Mono 2.11.3 发布包含微软的 Entity Framework
    系统架构设计师
    对数据访问层的抽象中
    说说项目中的插件思想
  • 原文地址:https://www.cnblogs.com/wsine/p/5181604.html
Copyright © 2011-2022 走看看