dcsimg
DNS client using jsp
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   roy_tsang
Posted On:   Thursday, October 3, 2002 09:16 PM

I am writing a DNS client using JSP. Can you tell me why I alway get "format error" from server response? Here is my code: <%@page language="java" import="java.io.*"%> <%@page language="java" import="javax.servlet.*"%> <%@page language="java" import="java.net.*"%> <%@page language="java" import="java.util.*"%> <% InetAddress nameServer = InetAddress.getByName("myDNS server IP address"); short id=10; short type=Short.parseShort("0000000110000000",2); short qCount=1; short aCount=0;    More>>

I am writing a DNS client using JSP.

Can you tell me why I alway get "format error" from server response?

Here is my code:
<%@page language="java" import="java.io.*"%>

<%@page language="java" import="javax.servlet.*"%>

<%@page language="java" import="java.net.*"%>

<%@page language="java" import="java.util.*"%>

<%

InetAddress nameServer = InetAddress.getByName("myDNS server IP address");

short id=10;

short type=Short.parseShort("0000000110000000",2);

short qCount=1;

short aCount=0;

short nsCount=10;

short arCount=1;

String queryHost="www.yahoo.com";

short qType=1;

short qClass=1;

int i;

byte[] buf;

ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream ();

DataOutputStream dataOut = new DataOutputStream (byteArrayOut);

dataOut.writeShort(id);

dataOut.writeShort(type);

dataOut.writeShort(qCount);

dataOut.writeShort(aCount);

dataOut.writeShort(nsCount);

dataOut.writeShort(arCount);

StringTokenizer labels = new StringTokenizer (queryHost, ".");

while (labels.hasMoreTokens ())

 {

  String label = labels.nextToken ();

  dataOut.writeByte (label.length ());

  dataOut.writeBytes (label);

 }

dataOut.writeByte (0);

dataOut.writeShort (qType);

dataOut.writeShort (qClass);

buf=byteArrayOut.toByteArray ();

Socket socket = new Socket (nameServer, 53);

BufferedOutputStream bufferedOut = new BufferedOutputStream (socket.getOutputStream ());

DataOutputStream socketOut = new DataOutputStream (bufferedOut);

socket.setSoTimeout (10000);

socketOut.writeShort (buf.length);

socketOut.write (buf);

socketOut.flush();

InputStream bufferedIn = new BufferedInputStream (socket.getInputStream ());

DataInputStream socketIn = new DataInputStream (bufferedIn);

int responseLength = socketIn.readUnsignedShort ();

byte[] data = new byte[responseLength];

socketIn.readFully (data);

FileOutputStream fos=new FileOutputStream("e:\html\testing\output.bin");

fos.write(data);

bufferedOut.close();

socketOut.close();

bufferedIn.close();

socketIn.close();

socket.close();

%>

   <<Less

Re: DNS client using jsp

Posted By:   Jorgen_Nordqvist  
Posted On:   Monday, October 7, 2002 03:41 PM

From RFC1035:


4.2. Transport

The DNS assumes that messages will be transmitted as datagrams or in a
byte stream carried by a virtual circuit. While virtual circuits can be
used for any DNS activity, datagrams are preferred for queries due to
their lower overhead and better performance. Zone refresh activities
must use virtual circuits because of the need for reliable transfer.

The Internet supports name server access using TCP [RFC-793] on server
port 53 (decimal) as well as datagram access using UDP [RFC-768] on UDP
port 53 (decimal).


Seems like it is possible some name servers support UDP only and some also TCP. You may want to try opening a DatagramSocket so that your query uses UDP instead.

Jorgen

About | Sitemap | Contact