什么IP用于自我testing?

一堆服务器提供特定端口的function(端口监听所有接口)。 服务器必须通过连接自己来自我testing自己的function。 testing连接使用什么DNS名称或IP?

理想情况下,我会使用本地IP地址之一(例如'192.13.1.5')。 但是,获取本地IP列表对于testing代码来说是困难的。

我可以使用'localhost'(127.0.0.1),但是它使用了回送接口并绕过了networking硬件。 这导致我…

问题: “这个服务器使用的任何本地IP”是否有简写符号? (除了回环地址)。

使用'0.0.0.0'似乎可以在Linux上使用,但在Windows上则不行。

为了增加迈克尔·汉普顿的build议,Windows应该将%computername%识别为可以ping的环境variables等。

它看起来像PowerShell cmdlet Test-NetConnection和Windows环境variables可能会做你想做的。 你可以指定一个端口,但是因为你没有提到一个我刚刚使用的端口

 Test-NetConnection -ComputerName $env:COMPUTERNAME 

这在我的笔记本电脑上工作得很好。 检查端口135的端口语法(作为一个随机的例子)将是

 Test-NetConnection -Port 135 -ComputerName $env:COMPUTERNAME 

谢谢你们 – 非常感谢你们的帮助。 是的,主机名环境variables可以工作,但需要访问代码。 我与沙恩 – 0.0.0.0 应该工作,但有一些棘手的事情。

我想我需要创build代码(这必须是Java),所以我写这个来获得三套IP:

  List<String> IPAddress = new ArrayList<String>(); List<String> NonLoopbackIPv4Address = new ArrayList<String>(); List<String> NonLoopbackIPv6Address = new ArrayList<String>(); try { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while(en.hasMoreElements()){ NetworkInterface ni=(NetworkInterface) en.nextElement(); Enumeration<InetAddress> ea = ni.getInetAddresses(); while(ea.hasMoreElements()) { InetAddress ia= (InetAddress) ea.nextElement(); //Add entries to the overall address array IPAddress.add(ia.getHostAddress()); //Add entries to non-loopback IPv4 address array if (!ia.isLoopbackAddress() && (ia.getClass() == Inet4Address.class)) { NonLoopbackIPv4Address.add(ia.getHostAddress()); } //Add entries to non-loopback IPv6 address array if (!ia.isLoopbackAddress() && (ia.getClass() == Inet6Address.class)) { NonLoopbackIPv6Address.add(ia.getHostAddress()); } } } } catch (Exception e) { //catch and process necessary exceptions, etc... throw new Exception (e); } 

…哪里:

  • IPAddress – 此主机(包括v4和v6)的所有IP地址列表,包括绑定到回送接口的那些IP地址
  • NonLoopbackIPv4Address – 此主机的所有IPv4地址的列表,不包括绑定到回送接口的IPv4地址
  • NonLoopbackIPv6Address – 此主机的所有IPv6地址列表,不包括绑定到回送接口的IPv6地址