Java InetAddress.getByName返回错误的IP地址

Java InetAddress.getByName返回错误的IP地址,java,serversocket,Java,Serversocket,我一直在服务器上使用以下代码 public SocketServer(int port,String inetAddress) throws IOException { this.port = port; this.ia = InetAddress.getByName(inetAddress); log.info(String.format("Internet Address %s using port %d for provided IP A

我一直在服务器上使用以下代码

public SocketServer(int port,String inetAddress) throws IOException {
        this.port = port;
        this.ia = InetAddress.getByName(inetAddress);
        log.info(String.format("Internet Address %s using port  %d  for provided IP Address  %s", this.ia.toString() ,this.port ,inetAddress.toString()));
        s = new ServerSocket(port,50,this.ia );
    }
这在我的本地服务器上运行良好,但在生产环境中它提供了错误的地址。 生产服务器不包含以下类型的IP:-

  • 私有IP
  • VPN IP
  • 公共知识产权
我提供私有IP,并希望服务器使用该私有IP进行连接,但它使用VPN IP进行连接

我还想做的一件事是使用InetAddress.getByAddress(),但我无法将字符串中的IP转换为字节数组。
有人能给我提供这方面的解决方案吗?

如果我没有弄错的话,这可能是与DNS相关的问题。getByName(字符串主机)将返回分配给特定域名的第一个IP地址

也就是说,如果在/etc/hosts文件中有这样的内容

192.168.1.1  sandbox1
192.168.1.2  sandbox1
代码

InetAddress.getByName("sandbox1")
将始终为您提供192.168.1.1


希望这有帮助

执行getByAddress以查看它是否正确,即
this.ia=InetAddress.getByAddress(InetAddress)
是的,我已尝试执行上述问题中提到的相同操作。但我无法将原始字符串“10.20.30.40”转换为GetByAddress可以接受的正确字节数组。是的,我也在考虑同样的问题,甚至考虑使用getAllByName来获取InetAddress数组。但我也尝试使用InetAddress.getByAddress,但我不能使用它,因为它需要字节数组;但是是的,就是这样。请注意,使用
InetAddress
.getAllByName()
方法可以从名称中获取所有地址。在这种情况下,有人可以建议使用getByAddress吗?@hellowahab这里的问题似乎是系统配置问题;您有一个相同的机器名,可以解析为不同的地址,这是可以理解的;但这个相同的名称似乎也可以解析为完全不相关的地址,这是一个配置问题。例如,为什么一个相同的名称会同时解析为公共接口地址和VPN接口地址?@fge我在getByName中提供了类似“10.10.10.10”的原始IP我没有提供像abc.com这样的主机名我的期望是它将返回与InetAddress相同的原始IP,但它似乎相反返回了VPN IP。我正在考虑使用GetByAddress,但由于参数不同而无法使用它