Java中的线程DNS服务器
我正在尝试用java创建一个简单的DNS服务器。这是我的密码:Java中的线程DNS服务器,java,dns,Java,Dns,我正在尝试用java创建一个简单的DNS服务器。这是我的密码: public class DNSServer{ public static final int DEFAULT_PORT = 6052; private static ServerSocket sock = null; static Socket client; public static void main(String[] args) {
public class DNSServer{
public static final int DEFAULT_PORT = 6052;
private static ServerSocket sock = null;
static Socket client;
public static void main(String[] args)
{
try
{
ServerSocket sock = new ServerSocket(DEFAULT_PORT);
/* now listen for connections */
while(true)
{
System.out.println("Server is listening...\n");
client = sock.accept();
System.out.println("Client connected\n");
// create a separate thread
// to service the request
Thread worker = new Thread(new Connection(client));
worker.start();
}
}
catch (IOException ioe) {
System.err.println(ioe);
}
finally {
//close Socket
try {
client.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
}
}
}
}
}
但是,当我提供一个参数url并按enter键时,程序就会卡住。。
有什么问题吗?我做错了什么
我们将不胜感激。
多谢各位
编辑:您是否尝试过使用调试器单步调试代码?不,我们的讲师说调试在这里没有用,因为我们通过命令提示符运行所有程序。您不必通过命令提示符运行所有程序。说真的,用调试器一步一步地完成这个过程——你会发现这个过程非常有用。如果你不愿意使用IDE和调试器,至少要添加更多的日志记录并将输出添加到你的帖子中。你应该证明你理解你所问的主题,而不仅仅是复制/粘贴别人的代码。
public class Connection implements Runnable {
private Socket client=null;
private BufferedReader fromClient = null;
private PrintWriter toClient = null;
private String hostAddress;
public Connection(Socket client)
{
this.client=client;
}
@Override
public void run()
{
try
{
fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
toClient = new PrintWriter(client.getOutputStream(), true);
String nameRequest = fromClient.readLine(); //Reading from Socket
InetAddress hostInetAddress = InetAddress.getByName(nameRequest);
hostAddress = hostInetAddress.getHostAddress();
toClient.println(hostAddress); //Writing the Result IP address (as a String) to the Socket
}
catch(UnknownHostException uhe){
hostAddress = "Unknown Host";
}
catch(IOException ioe){
System.err.println(ioe);
}
finally
{
try {
fromClient.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
toClient.close();
try {
client.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
}
}
public class DNSClient {
public static final int DEFAULT_PORT = 6052;
private static Socket sock;
private static BufferedReader fromServer;
private static PrintWriter toServer;
public static void main(String[] args){
if(args.length==0)
{
System.out.println("Usage: java DNSClient [host name]");
System.exit(1);
}
else
{
try
{
sock = new Socket("127.0.0.1", DEFAULT_PORT );
toServer = new PrintWriter(args[0]); //send hostname to server
fromServer = new BufferedReader(new InputStreamReader(sock.getInputStream())); //read host IP from server
//print output
System.out.println(args[0] + " = " + fromServer.readLine());
}
catch(IOException ioe){
System.err.println(ioe);
}
finally{
toServer.close();
try {
fromServer.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
try {
sock.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
}
}
}