我用Java编写了这个程序,我在Ubuntu 14下运行它。
运行时,它通过端口53将UDP数据包发送到在Debian 8.0下运行的缓存DNS服务器。
服务器向源发送回复。如何修改程序以使服务器在同一LAN中回复另一个IP地址?
以下是该计划:
import java.io.*;
import java.net.*;
public class ClientSender {
private static Object InetAdress;
public static void main(String[] args) {
int i ;
for (i=0;i<1000;i++){
DatagramSocket skt;
try{
/////////////////////////////////////////
skt = new DatagramSocket(); /////creating socket
String msg = "Test";//////////message
byte[] b =msg.getBytes(); /// creating message
InetAddress host =InetAddress.getByName("192.168.1.3"); ///destination IP address
int serverSocket = 53; // Destination Port
///////////////////////////////////////////////////////////////
DatagramPacket request =new DatagramPacket(b,b.length,host,serverSocket);
skt.send(request); /////////////////////////creating packet
///////////////////////////////////////////////////////////////////////////////
//..................................// for receiving replies from destnation
byte [] buffer = new byte[1000];
DatagramPacket reply = new DatagramPacket (buffer, buffer.length);
skt.receive(reply); ///////////////////////////////////lestining to replies from destination
skt.close();///////////////closing socket
System.out.println(new String(request.getData()));
System.out.println(new String(reply.getData()));
}
catch (Exception ex)
{
}
}}
}