Search in sources :

Example 1 with DatagramDnsResponse

use of io.netty.handler.codec.dns.DatagramDnsResponse in project netty by netty.

the class DnsClient method main.

public static void main(String[] args) throws Exception {
    InetSocketAddress addr = new InetSocketAddress(DNS_SERVER_HOST, DNS_SERVER_PORT);
    EventLoopGroup group = new NioEventLoopGroup();
    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();
    }
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) InetSocketAddress(java.net.InetSocketAddress) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DatagramChannel(io.netty.channel.socket.DatagramChannel) Channel(io.netty.channel.Channel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPipeline(io.netty.channel.ChannelPipeline) DatagramDnsQueryEncoder(io.netty.handler.codec.dns.DatagramDnsQueryEncoder) DatagramDnsResponse(io.netty.handler.codec.dns.DatagramDnsResponse) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DatagramDnsQuery(io.netty.handler.codec.dns.DatagramDnsQuery) Bootstrap(io.netty.bootstrap.Bootstrap) DnsQuery(io.netty.handler.codec.dns.DnsQuery) DatagramDnsQuery(io.netty.handler.codec.dns.DatagramDnsQuery) DatagramDnsResponseDecoder(io.netty.handler.codec.dns.DatagramDnsResponseDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 DatagramChannel (io.netty.channel.socket.DatagramChannel)1 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)1 DatagramDnsQuery (io.netty.handler.codec.dns.DatagramDnsQuery)1 DatagramDnsQueryEncoder (io.netty.handler.codec.dns.DatagramDnsQueryEncoder)1 DatagramDnsResponse (io.netty.handler.codec.dns.DatagramDnsResponse)1 DatagramDnsResponseDecoder (io.netty.handler.codec.dns.DatagramDnsResponseDecoder)1 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)1 DnsQuery (io.netty.handler.codec.dns.DnsQuery)1 InetSocketAddress (java.net.InetSocketAddress)1