Was ich jetzt habe, ist DNS-Abfrage über UDP und es funktioniert ordnungsgemäß, aber wenn Nachricht abgeschnitten wird und ich über TCP mit der gleichen Abfrage erneut verbinden muss, kann ich das nicht tun, Hauptproblem ist, dass ich es tue habe Abfrage in Byte-Array und tcp sendet Zeichen.DNS-Abfrage über TCP
Meine Montage von UDP-Abfrage:
String DNS_SERVER_ADDRESS = args[0];
String domain = args[1];
ipAddress = InetAddress.getByName(DNS_SERVER_ADDRESS);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
dos = new DataOutputStream(baos);
// *** Build a DNS Request Frame ****
// Identifier: A 16-bit identification field generated by the device that creates the DNS query.
// It is copied by the server into the response, so it can be used by that device to match that
// query to the corresponding reply received from a DNS server. This is used in a manner similar
// to how the Identifier field is used in many of the ICMP message types.
dos.writeShort(0x1234);
// Write Query Flags
dos.writeShort(0x0100);
// Question Count: Specifies the number of questions in the Question section of the message.
dos.writeShort(0x0001);
// Answer Record Count: Specifies the number of resource records in the Answer section of the message.
dos.writeShort(0x0000);
// Authority Record Count: Specifies the number of resource records in the Authority section of
// the message. (“NS” stands for “name server”)
dos.writeShort(0x0000);
// Additional Record Count: Specifies the number of resource records in the Additional section of the message.
dos.writeShort(0x0000);
// TODO: write query
String[] domainParts = domain.split("\\.");
System.out.println(domain + " has " + domainParts.length + " parts");
for (String domainPart : domainParts) {
System.out.println("Writing: " + domainPart);
byte[] domainBytes = domainPart.getBytes("UTF-8");
dos.writeByte(domainBytes.length);
dos.write(domainBytes);
}
// No more parts
dos.writeByte(0x00);
// QType 0x01 = A (Host Request)
if (args.length>2)
dos.writeShort(typeEncode(args[2]));
else
dos.writeShort(0x00ff); // "ANY" as default
// QClass 0x01 = IN
dos.writeShort(0x0001);
dnsFrame = baos.toByteArray();
System.out.println("Sending: " + dnsFrame.length + " bytes");
for (byte aDnsFrame : dnsFrame) {
System.out.print("0x" + String.format("%x", aDnsFrame) + " ");
}
// *** Send DNS Request Frame ***
DatagramSocket socket = new DatagramSocket();
DatagramPacket dnsReqPacket = new DatagramPacket(dnsFrame, dnsFrame.length, ipAddress, DNS_SERVER_PORT);
socket.send(dnsReqPacket);
return socket;
Wie ich versuche es über TCP zu senden:
Socket echoSocket = null;
// strumień do zapisu do serwera
Writer out = null;
// strumień do odczytu z serwera
BufferedReader in = null;
// nazwa serwera
String hostname=args[0];
try {
System.out.println("próba utworzenia gniazda");
echoSocket = new Socket(ipAddress, DNS_SERVER_PORT);
System.out.println("próba utworzenia strumienia wyjściowego");
out = new PrintWriter(echoSocket.getOutputStream(), true);
OutputStream outputStream = echoSocket.getOutputStream();
System.out.println("próba utworzenia strumienia wejściowego");
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
ObjectOutputStream os = new ObjectOutputStream(echoSocket.getOutputStream());
os.flush();
ObjectInputStream is = new ObjectInputStream(echoSocket.getInputStream());
os.writeObject(dnsFrame);
byte[] temp = (byte[]) is.readObject();
} catch (UnknownHostException e) {
System.err.println("Nieznany host: " + hostname + ".");
System.exit(1);
} catch (IOException e) {
System.err.println("Błąd połączenia z " + hostname + ".");
System.exit(1);
}
// zakończenie pracy - pozamykaj strumienie i gniazda
out.close();
in.close();
echoSocket.close();
ich wireshark verwendet zu lesen, was in TCP sende ich tatsächlich, und es ist nicht das gleiche Als UDP nehme ich an, dass das Problem gerade darin besteht, ein Byte-Array zu senden.
_ "tcp sendet Zeichen" _ - was macht Sie denken, ist das wahr? Ein 'ObjectOutputStream' ist für Java-Objekte gedacht und unterscheidet sich sehr von einem 'DataOutputStream'. Ihre TCP- und UDP-Versionen unterscheiden sich sehr voneinander. –
TCP sendet * Oktette *. Sie sollten 'Reader' und' Writers' nicht verwenden, Sie sollten den gleichen 'DataInput/OutputStream' verwenden wie bei YDP, in der Tat sehr ähnlich, aber mit' Socket.getInput/OutputStream() 'als Basiswert Stream statt die 'ByteArrayInput/OutputStreams.' – EJP