• 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();
    		}
    	}
    }
    

    运行截图




  • 相关阅读:
    linux查看文件夹里文件个数
    image.shape[0]、[1]分别是高和宽
    cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
    unzip解压到指定目录
    pycharm上传文件到远程服务器失败原因
    CSS – 网页设计 Web Design
    CSS – Sass & SCSS
    CSS – RWD (Responsive Web Design) 概念篇
    CSS – Icon
    CSS – wordbreak, overflowwrap, wordwrap, whitespace
  • 原文地址:https://www.cnblogs.com/wsine/p/5181604.html
Copyright © 2020-2023  润新知