集册 Java实例教程 Whois查询

Whois查询

欢马劈雪     最近更新时间:2020-01-02 10:19:05

430
Whois查询


import java.io.IOException;

import java.net.InetAddress;//nowjava.com - 时  代  Java

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.SocketChannel;

import java.util.regex.Pattern;


public class WhoisQuery {


    public static void main(String[] args) {

        if (args.length < 1) {

            System.err.println("Usage: java WhoisQuery [port] host...");

            return;

        }


        int port = 43;

        int firstArg = 0;

        if (Pattern.matches("[0-9]+", args[0])) {

            port = Integer.parseInt(args[0]);

            ++firstArg;

        }/** from N o w  J a v a  .   c o m**/


        for (int i = firstArg; i < args.length; i++) {

            String host = args[i];


            try {

                query(host, port);

            } catch (IOException e) {

                System.err.println(host + ": " + port + "=" + e);

            }


        }


    }


    private static void query(String host, int port) throws IOException {

        InetAddress i = InetAddress.getByName("whois.internic.net");

        InetSocketAddress isa = new InetSocketAddress(i, port);

        ByteBuffer buf = ByteBuffer.allocateDirect(2048);


        try (SocketChannel sc = SocketChannel.open()) {

            sc.connect(isa);


            buf.clear();

            buf.put(host.getBytes());

            buf.putChar('\r');

            buf.putChar('\n');

            buf.flip();

            sc.write(buf);


            buf.clear();

            
展开阅读全文