我在一个简单的DNS服务器上工作。我也喜欢自己做,因为它也是一种学习经验。但是,我一直试图了解DNS请求。问题是,数据(作为字符串)出现了一些我不知道如何处理的额外的不可读字符。我什至期望像“ WhoIs google.com响应代码1234”之类的内容,但能在屏幕截图中看到 caption after run 还有我的源代码。
package com.donsoft.ambient;
import java.net.*;
public class Dns {
public static void main(String[] args){
try{
DatagramSocket sock = new DatagramSocket(53);
//Now for reading the packets
while(true){
byte[] buffer = new byte[256]; //I think that size is good
DatagramPacket in = new DatagramPacket(buffer, buffer.length);
sock.receive(in);
String data = new String(in.getData());
if(data!=null){
process(in);
}
Thread.sleep(200); //To reduce CPU usage
}
}catch(Exception error){error.printStackTrace();}
}
public static void process(DatagramPacket pack){
//I just print it out
//But this is supposed to read the query and respond
//Am hooked at the point of formatting the data
String data = new String(pack.getData());
System.out.println(data);
}
}