简体   繁体   English

如何在Java中解析远程计算机名称以获取IP地址

[英]How to resolve a remote computer name to get it IP address in java

I'm trying to come up with a program that send packets from computer A to computer B . 我正在尝试提出一个程序,将数据包从计算机A发送到计算机B。 both computers must have a copy of my java program. 两台计算机都必须有我的Java程序的副本。 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. 这要求在JTextField对象中输入远程计算机B的名称,在JTextArea对象中输入消息,然后单击“发送”按钮。

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. 我的程序应该能够将给定的计算机名称解析为ip地址,以便将IP地址作为参数包含在DatagramPacket构造函数中。

I have tried using the below method to do the resolving but i get a javax.naming.CommunicationException . 我尝试使用以下方法进行解析,但是得到了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");

Exception thrown 抛出异常

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

Question

How can i achieve my goal coz it seems to me like java DNS service provider can only resolve domain names and not individual computer names? 我怎么能实现我的目标,因为在我看来,java DNS服务提供商只能解析域名,而不能解析单个计算机名? i've been struggling with this for 3 days. 我已经为此苦苦挣扎了3天。

Any help is appreciated. 任何帮助表示赞赏。

Use of JNDI is only useful if you need specific DNS attributes/entries. 仅当您需要特定的DNS属性/条目时,才使用JNDI

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. 将使用本地DNS基础结构-就像它的操作系统一样-您不需要自己提供DNS服务器。

http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html 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 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. 给定主机名,确定主机的IP地址。

The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. 主机名可以是机器名称,例如“ java.sun.com”,也可以是其IP地址的文本表示。 If a literal IP address is supplied, only the validity of the address format is checked. 如果提供文字IP地址,则仅检查地址格式的有效性。

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地址指定的主机,可接受RFC 2732中定义的格式或RFC 2373中定义的文字IPv6地址格式。 IPv6 scoped addresses are also supported. 还支持IPv6范围的地址。

Did you read http://docs.oracle.com/javase/7/docs/technotes/guides/jndi/jndi-dns.html ? 您是否阅读了http://docs.oracle.com/javase/7/docs/technotes/guides/jndi/jndi-dns.html

It seams like: 它的接缝像:

Hashtable env = new Hashtable();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url",    "dns://google-public-dns-a.google.com");

DirContext ictx = new InitialDirContext(env);
Attributes attrs1 = ictx.getAttributes("clientname", new String[] {"A"});

is what you want. 是你想要的。

The timeout values are configurable either: 超时值是可以配置的:

env.put("com.sun.jndi.dns.timeout.initial", "2000"); env.put(“ com.sun.jndi.dns.timeout.initial”,“ 2000”);

env.put("com.sun.jndi.dns.timeout.retries", "3"); env.put(“ com.sun.jndi.dns.timeout.retries”,“ 3”);

Use FQDN (full qualified domain name) if you will use a public DNS server instead of clientname like shown in your Exception. 使用FQDN(完全限定域名),如果你会使用公共DNS服务器,而不是clientname状显示在您的例外。 Google doesn't hopefully know anything about clientname ;-). 谷歌不希望一无所知clientname ;-)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM