Virtualbox 与获取 ip 冲突
Virtualbox conflicting with getting ip
我在安装 oracle 的 virtualbox 后 return 遇到了正确 IP 的问题。它打印如下:
VirtualBox Host-Only Ethernet Adapter 192.168.56.1
VirtualBox Host-Only Ethernet Adapter fe30:0:0:0:1323:fahd:bt75:8422%eth1
Microsoft Teredo Tunneling Adapter 2041:0:91q8:6at8:30he:3r2c:3a53:ff4c
Microsoft Teredo Tunneling Adapter fj80:0:0:0:32bn:1e2z:3f37:ff5c%net4
Realtek PCIe GBE Family Controller 192.168.0.163
Realtek PCIe GBE Family Controller fe30:0:0:0:3a4c:bf90:232a:a324%eth6
我只想return192.168.0.163
我使用此代码获取 IP:
String ip;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
// filters out 127.0.0.1 and inactive interfaces
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ip = addr.getHostAddress();
System.out.println(iface.getDisplayName() + " " + ip);
}
}
} catch (SocketException es) {
throw new RuntimeException(es);
}
如何只检索想要的 IP?
要在没有 IP 的情况下进行通信,最好的方法是通过发现服务或广播 UDP 数据包。
您将需要一些东西:
- 服务器在特定端口上侦听广播数据包,
或一般。
- 一个客户端(在本例中是您的 android 应用程序)监听特定的
连接端口(TCP/UDP 取决于您的应用程序需求)。
- 客户端还需要能够在网络上发送广播数据包来标识自己。
基本上步骤如下:
- 服务器绑定 UDP 广播接收所需的端口。
- 客户端绑定端口以接收来自服务器的响应(例如端口 16000)
- 然后客户端在网络上发送广播包,发送给所有客户端。
- 服务器接收数据包,并通过数据包信息获取客户端的 IP 地址。
- 然后服务器通过监听端口(在本例中为 16000)通过 UDP 或 TCP 响应客户端。
- 一旦建立与客户端的连接,客户端就会通过连接信息知道服务器的 IP,与服务器建立有效连接,并可能保存地址供以后使用。
技术细节可以在 Whosebug 上找到,但它会根据 server/client 的编程语言和涉及的操作系统而有所不同。
这是 Java 的 Oracle 文档教程:
https://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html
一个用于 C#:
https://msdn.microsoft.com/en-us/library/tst0kwb1(v=vs.110).aspx
我在安装 oracle 的 virtualbox 后 return 遇到了正确 IP 的问题。它打印如下:
VirtualBox Host-Only Ethernet Adapter 192.168.56.1
VirtualBox Host-Only Ethernet Adapter fe30:0:0:0:1323:fahd:bt75:8422%eth1
Microsoft Teredo Tunneling Adapter 2041:0:91q8:6at8:30he:3r2c:3a53:ff4c
Microsoft Teredo Tunneling Adapter fj80:0:0:0:32bn:1e2z:3f37:ff5c%net4
Realtek PCIe GBE Family Controller 192.168.0.163
Realtek PCIe GBE Family Controller fe30:0:0:0:3a4c:bf90:232a:a324%eth6
我只想return192.168.0.163
我使用此代码获取 IP:
String ip;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
// filters out 127.0.0.1 and inactive interfaces
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ip = addr.getHostAddress();
System.out.println(iface.getDisplayName() + " " + ip);
}
}
} catch (SocketException es) {
throw new RuntimeException(es);
}
如何只检索想要的 IP?
要在没有 IP 的情况下进行通信,最好的方法是通过发现服务或广播 UDP 数据包。
您将需要一些东西:
- 服务器在特定端口上侦听广播数据包, 或一般。
- 一个客户端(在本例中是您的 android 应用程序)监听特定的 连接端口(TCP/UDP 取决于您的应用程序需求)。
- 客户端还需要能够在网络上发送广播数据包来标识自己。
基本上步骤如下:
- 服务器绑定 UDP 广播接收所需的端口。
- 客户端绑定端口以接收来自服务器的响应(例如端口 16000)
- 然后客户端在网络上发送广播包,发送给所有客户端。
- 服务器接收数据包,并通过数据包信息获取客户端的 IP 地址。
- 然后服务器通过监听端口(在本例中为 16000)通过 UDP 或 TCP 响应客户端。
- 一旦建立与客户端的连接,客户端就会通过连接信息知道服务器的 IP,与服务器建立有效连接,并可能保存地址供以后使用。
技术细节可以在 Whosebug 上找到,但它会根据 server/client 的编程语言和涉及的操作系统而有所不同。
这是 Java 的 Oracle 文档教程: https://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html
一个用于 C#: https://msdn.microsoft.com/en-us/library/tst0kwb1(v=vs.110).aspx