日期:2014-05-17 浏览次数:20719 次
InetAddress bind_addr = InetAddress.getByName(DNSServer_UDP.BIND_ADDRESS); serverSocket = new DatagramSocket(DNSServer_UDP.BIND_PORT, bind_addr); while (bListerning) { System.out.println("\nUDP Listening...\n"); byte receiveData[] = new byte[DNSServer_UDP.PACKETBUFFSIZE]; DatagramPacket receivePacket = new DatagramPacket(receiveData, DNSServer_UDP.PACKETBUFFSIZE); // Receive a packet serverSocket.receive(receivePacket); // Print client IP and port System.out.println("Accept one: IP = " + receivePacket.getAddress().getHostAddress() + ", Port = " + receivePacket.getPort()); // Get the data ONSRecordAction recordAction = new ONSRecordAction(); ByteArrayInputStream bin = new ByteArrayInputStream(receiveData); ObjectInputStream oin = new ObjectInputStream(bin); recordAction = (ONSRecordAction)oin.readObject(); bin.close(); oin.close(); // Process the request in a new thread and keep listening in the port to get new packet new ServerSubThread_UDP(recordAction, receivePacket.getAddress().getHostAddress(), receivePacket.getPort()).start(); } }