TcpDnsServer Class — netty Architecture
Architecture documentation for the TcpDnsServer class in TcpDnsServer.java from the netty codebase.
Entity Profile
Dependency Diagram
graph TD 2163a46b_2065_a299_066e_84c6fa29ec99["TcpDnsServer"] 20acd595_152f_e8d3_d196_9d7638210b10["TcpDnsServer.java"] 2163a46b_2065_a299_066e_84c6fa29ec99 -->|defined in| 20acd595_152f_e8d3_d196_9d7638210b10 e2c04177_18f3_85bf_661f_a4c1ab3458d3["main()"] 2163a46b_2065_a299_066e_84c6fa29ec99 -->|method| e2c04177_18f3_85bf_661f_a4c1ab3458d3 1404dde2_6698_e33d_44ec_c96c503e47f2["clientQuery()"] 2163a46b_2065_a299_066e_84c6fa29ec99 -->|method| 1404dde2_6698_e33d_44ec_c96c503e47f2 2c8b9319_0bcc_d629_b998_8b21ecf245a6["handleQueryResp()"] 2163a46b_2065_a299_066e_84c6fa29ec99 -->|method| 2c8b9319_0bcc_d629_b998_8b21ecf245a6
Relationship Graph
Source Code
example/src/main/java/io/netty/example/dns/tcp/TcpDnsServer.java lines 55–168
public final class TcpDnsServer {
private static final String QUERY_DOMAIN = "www.example.com";
private static final int DNS_SERVER_PORT = 53;
private static final String DNS_SERVER_HOST = "127.0.0.1";
private static final byte[] QUERY_RESULT = new byte[]{(byte) 192, (byte) 168, 1, 1};
public static void main(String[] args) throws Exception {
ServerBootstrap bootstrap = new ServerBootstrap().group(
new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory()))
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new TcpDnsQueryDecoder(), new TcpDnsResponseEncoder(),
new SimpleChannelInboundHandler<DnsQuery>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx,
DnsQuery msg) throws Exception {
DnsQuestion question = msg.recordAt(DnsSection.QUESTION);
System.out.println("Query domain: " + question);
//always return 192.168.1.1
ctx.writeAndFlush(newResponse(msg, question, 600, QUERY_RESULT));
}
private DefaultDnsResponse newResponse(DnsQuery query,
DnsQuestion question,
long ttl, byte[]... addresses) {
DefaultDnsResponse response = new DefaultDnsResponse(query.id());
response.addRecord(DnsSection.QUESTION, question);
for (byte[] address : addresses) {
DefaultDnsRawRecord queryAnswer = new DefaultDnsRawRecord(
question.name(),
DnsRecordType.A, ttl, Unpooled.wrappedBuffer(address));
response.addRecord(DnsSection.ANSWER, queryAnswer);
}
return response;
}
});
}
});
final Channel channel = bootstrap.bind(DNS_SERVER_PORT).channel();
Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() {
@Override
public void run() {
try {
clientQuery();
channel.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 1000, TimeUnit.MILLISECONDS);
channel.closeFuture().sync();
}
// copy from TcpDnsClient.java
private static void clientQuery() 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) {
ch.pipeline().addLast(new TcpDnsQueryEncoder())
.addLast(new TcpDnsResponseDecoder())
.addLast(new SimpleChannelInboundHandler<DefaultDnsResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, DefaultDnsResponse msg) {
try {
handleQueryResp(msg);
} finally {
ctx.close();
}
}
});
}
Source
Frequently Asked Questions
What is the TcpDnsServer class?
TcpDnsServer is a class in the netty codebase, defined in example/src/main/java/io/netty/example/dns/tcp/TcpDnsServer.java.
Where is TcpDnsServer defined?
TcpDnsServer is defined in example/src/main/java/io/netty/example/dns/tcp/TcpDnsServer.java at line 55.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free