Home / Class/ DnsClient Class — netty Architecture

DnsClient Class — netty Architecture

Architecture documentation for the DnsClient class in DnsClient.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  c3c638e5_9ebc_2b4f_b4ee_c21c8f26e50f["DnsClient"]
  c8168107_36d8_e33e_3aa3_3c820381f2f9["DnsClient.java"]
  c3c638e5_9ebc_2b4f_b4ee_c21c8f26e50f -->|defined in| c8168107_36d8_e33e_3aa3_3c820381f2f9
  c0f8c6a6_8527_da15_1f35_2dfeee4c221a["DnsClient()"]
  c3c638e5_9ebc_2b4f_b4ee_c21c8f26e50f -->|method| c0f8c6a6_8527_da15_1f35_2dfeee4c221a
  306eda2d_b52c_fcba_5c5b_08402e1d1f90["handleQueryResp()"]
  c3c638e5_9ebc_2b4f_b4ee_c21c8f26e50f -->|method| 306eda2d_b52c_fcba_5c5b_08402e1d1f90
  d80cfba1_e8be_0f12_c3f4_b9fa5d605fef["main()"]
  c3c638e5_9ebc_2b4f_b4ee_c21c8f26e50f -->|method| d80cfba1_e8be_0f12_c3f4_b9fa5d605fef

Relationship Graph

Source Code

example/src/main/java/io/netty/example/dns/udp/DnsClient.java lines 46–108

public final class DnsClient {

    private static final String QUERY_DOMAIN = "www.example.com";
    private static final int DNS_SERVER_PORT = 53;
    private static final String DNS_SERVER_HOST = "8.8.8.8";

    private DnsClient() { }

    private static void handleQueryResp(DatagramDnsResponse msg) {
        if (msg.count(DnsSection.QUESTION) > 0) {
            DnsQuestion question = msg.recordAt(DnsSection.QUESTION, 0);
            System.out.printf("name: %s%n", question.name());
        }
        for (int i = 0, count = msg.count(DnsSection.ANSWER); i < count; i++) {
            DnsRecord record = msg.recordAt(DnsSection.ANSWER, i);
            if (record.type() == DnsRecordType.A) {
                //just print the IP after query
                DnsRawRecord raw = (DnsRawRecord) record;
                System.out.println(NetUtil.bytesToIpAddress(ByteBufUtil.getBytes(raw.content())));
            }
        }
    }

    public static void main(String[] args) throws Exception {
        InetSocketAddress addr = new InetSocketAddress(DNS_SERVER_HOST, DNS_SERVER_PORT);
        EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
             .channel(NioDatagramChannel.class)
             .handler(new ChannelInitializer<DatagramChannel>() {
                 @Override
                 protected void initChannel(DatagramChannel ch) throws Exception {
                     ChannelPipeline p = ch.pipeline();
                     p.addLast(new DatagramDnsQueryEncoder())
                     .addLast(new DatagramDnsResponseDecoder())
                     .addLast(new SimpleChannelInboundHandler<DatagramDnsResponse>() {
                        @Override
                        protected void channelRead0(ChannelHandlerContext ctx, DatagramDnsResponse msg) {
                            try {
                                handleQueryResp(msg);
                            } finally {
                                ctx.close();
                            }
                        }
                    });
                 }
             });
            final Channel ch = b.bind(0).sync().channel();
            DnsQuery query = new DatagramDnsQuery(null, addr, 1).setRecord(
                    DnsSection.QUESTION,
                    new DefaultDnsQuestion(QUERY_DOMAIN, DnsRecordType.A));
            ch.writeAndFlush(query).sync();
            boolean succ = ch.closeFuture().await(10, TimeUnit.SECONDS);
            if (!succ) {
                System.err.println("dns query timeout!");
                ch.close().sync();
            }
        } finally {
            group.shutdownGracefully();
        }
    }
}

Frequently Asked Questions

What is the DnsClient class?
DnsClient is a class in the netty codebase, defined in example/src/main/java/io/netty/example/dns/udp/DnsClient.java.
Where is DnsClient defined?
DnsClient is defined in example/src/main/java/io/netty/example/dns/udp/DnsClient.java at line 46.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free