简体   繁体   English

网络接口-VB.Net/Java

[英]Network Interface - VB.Net/Java

I have this snippet in Java: 我在Java中有以下代码段:

final InetAddress address = InetAddress.getLocalHost();
final NetworkInterface ni = NetworkInterface.getByInetAddress(address);
key = new String(ni.getHardwareAddress());

"key", on my PC returns: “ key”,在我的PC上返回:

▲╔UiÎ ▲╔UiÎ

What would be the equivalent to get the same output in VB.Net? 在VB.Net中获得相同输出的等效条件是什么? I have tried getting 'MAC Address' and 'Physical Address' but they do not compare to the output in Java at all? 我尝试获取“ MAC地址”和“物理地址”,但是它们根本无法与Java中的输出进行比较吗?

I can't help you with the VB, but maybe I can clear a few things up. 我无法帮助您使用VB,但也许我可以解决一些问题。

In my experience, InetAddress.getLocalHost() is useless. 以我的经验, InetAddress.getLocalHost()是没有用的。 It gives different results depending on the OS, on the Java version, on the brand of router you're connecting to, on the phase of the moon, etc. Stay away from it. 根据操作系统,Java版本,要连接的路由器品牌,月相等,它会给出不同的结果。请远离它。 If you want the loopback IP address, just hard-code it: 127.0.0.1. 如果您想要回送IP地址,只需对其进行硬编码:127.0.0.1。 If you want the addresses of the network interfaces on your machine, call NetworkInterface.getInterfaces() and iterate through them. 如果要在计算机上使用网络接口的地址,请调用NetworkInterface.getInterfaces()并对其进行迭代。

The MAC address is the unique ID of an ethernet interface. MAC地址是以太网接口的唯一ID。 Normally, you don't have to worry about it at all, unless you're doing something weird with the network hardware. 通常,除非您对网络硬件做一些奇怪的事情,否则根本不必担心。 It's got nothing to do with IP addresses. 与IP地址无关。

Lastly: if you're looking to determine the IP address that others can use to connect to you, just be aware that it's not so simple. 最后:如果您要确定其他人可以用来连接到您的IP地址,请注意,它并不是那么简单。 A computer may have multiple network interfaces - how do you know which one to pick? 一台计算机可能具有多个网络接口-您如何知道选择哪个接口? And if you're behind a router (as most machines are these days) then the computer is assigned a "local" address, which is completely useless to anyone outside the local network. 而且,如果您在路由器后面(因为如今大多数计算机都在使用路由器),则会为计算机分配一个“本地”地址,这对本地网络之外的任何人都是完全无用的。

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

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