I'm trying to come up with a program that send packets from computer A to computer B. Both computers must have a copy of my Java program. This require one to enter the name of the remote computer B in a JTextField
object, enter the message in a JTextArea
object and then click the button send.
My program should be able to resolve the given computer name to IP address so to include the IP address as a parameter in my DatagramPacket
constuctor.
I have tried using the below method to do the resolving, but I get a javax.naming.CommunicationException
:
String clientname = "user";
Hashtable<String,Object> env = new Hashtable<String,Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
env.put(Context.PROVIDER_URL, "dns://www.google.com");
DirContext con = new InitialDirContext(env);
Object obj = con.lookup("clientname");
javax.naming.CommunicationException: DNS error [Root exception is java.net.SocketTimeoutException: Receive timed out]; remaining name 'user'
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:300)
at com.sun.jndi.dns.Resolver.query(Resolver.java:81)
at com.sun.jndi.dns.DnsContext.c_lookup(DnsContext.java:286)
at com.sun.jndi.toolkit.ctx.ComponentContext.p_lookup(ComponentContext.java:544)
at com.sun.jndi.toolkit.ctx.PartialCompositeContext.lookup(PartialCompositeContext.java:177)
at com.sun.jndi.toolkit.ctx.PartialCompositeContext.lookup(PartialCompositeContext.java:166)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
Caused by: java.net.SocketTimeoutException: Receive timed out
at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.receive0(DualStackPlainDatagramSocketImpl.java:121)
at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:145)
at java.net.DatagramSocket.receive(DatagramSocket.java:786)
at com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:411)
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:203)
... 7 more
How can I achieve my goal, because it seems to me like Java DNS service provider can only resolve domain names and not individual computer names?
I've been struggling with this for 3 days.
Any help is appreciated.
Use of JNDI
is only useful if you need specific DNS attributes/entries.
Maybe the following is more suitable for you:
final InetAddress inetAddress = InetAddress.getByName("clientname");
final String ipAddress = inetAddress.getHostAddress();
The local DNS infrastructure - like the OS it does - will be use and you don't need to provide an DNS server by yourself.
http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html
http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#getByName%28java.lang.String%29
Determines the IP address of a host, given the host's name.
The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.
For host specified in literal IPv6 address, either the form defined in RFC 2732 or the literal IPv6 address format defined in RFC 2373 is accepted. IPv6 scoped addresses are also supported.