Home / Class/ TcpDnsClient Class — netty Architecture

TcpDnsClient Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  b78736d4_8edb_d42a_2963_35bd4229b033["TcpDnsClient"]
  f0f5c5b7_014e_e343_fc50_207e3e4beba9["TcpDnsClient.java"]
  b78736d4_8edb_d42a_2963_35bd4229b033 -->|defined in| f0f5c5b7_014e_e343_fc50_207e3e4beba9
  b97c25a2_b49b_b1c2_4d6b_1a067bdfeb69["TcpDnsClient()"]
  b78736d4_8edb_d42a_2963_35bd4229b033 -->|method| b97c25a2_b49b_b1c2_4d6b_1a067bdfeb69
  42611615_134a_300f_56c1_226f1fc075c0["handleQueryResp()"]
  b78736d4_8edb_d42a_2963_35bd4229b033 -->|method| 42611615_134a_300f_56c1_226f1fc075c0
  cd1c434a_06e1_fddb_85cd_040fdb61f8c4["main()"]
  b78736d4_8edb_d42a_2963_35bd4229b033 -->|method| cd1c434a_06e1_fddb_85cd_040fdb61f8c4

Relationship Graph

Source Code

example/src/main/java/io/netty/example/dns/tcp/TcpDnsClient.java lines 49–112

public final class TcpDnsClient {
    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 TcpDnsClient() {
    }

    private static void handleQueryResp(DefaultDnsResponse 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 {
        EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast(new TcpDnsQueryEncoder())
                                    .addLast(new TcpDnsResponseDecoder())
                                    .addLast(new SimpleChannelInboundHandler<DefaultDnsResponse>() {
                                        @Override
                                        protected void channelRead0(ChannelHandlerContext ctx, DefaultDnsResponse msg) {
                                            try {
                                                handleQueryResp(msg);
                                            } finally {
                                                ctx.close();
                                            }
                                        }
                                    });
                        }
                    });

            final Channel ch = b.connect(DNS_SERVER_HOST, DNS_SERVER_PORT).sync().channel();

            int randomID = new Random().nextInt(60000 - 1000) + 1000;
            DnsQuery query = new DefaultDnsQuery(randomID, DnsOpCode.QUERY)
                    .setRecord(DnsSection.QUESTION, new DefaultDnsQuestion(QUERY_DOMAIN, DnsRecordType.A));
            ch.writeAndFlush(query).sync();
            boolean success = ch.closeFuture().await(10, TimeUnit.SECONDS);
            if (!success) {
                System.err.println("dns query timeout!");
                ch.close().sync();
            }
        } finally {
            group.shutdownGracefully();
        }
    }
}

Frequently Asked Questions

What is the TcpDnsClient class?
TcpDnsClient is a class in the netty codebase, defined in example/src/main/java/io/netty/example/dns/tcp/TcpDnsClient.java.
Where is TcpDnsClient defined?
TcpDnsClient is defined in example/src/main/java/io/netty/example/dns/tcp/TcpDnsClient.java at line 49.

Analyze Your Own Codebase

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

Try Supermodel Free