javagethost的简单介绍
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
本篇文章给大家谈谈javagethost,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
- 1、java 怎么根据IP地址获取主机名
- 2、java中InetAddress的getAddress和getHostAddress有什么区别?
- 3、java中的getinetaddress和gethostaddress的区别
- 4、java如何获取网卡地址
- 5、java中怎么查看本机回传地址
- 6、Java如何通过网络进行寻找附近的设备,附源码?
java 怎么根据IP地址获取主机名
//看看这个代码如何。
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.Set;
public class TestSystemProperties {
public static void main(String [] args){
InetAddress netAddress = getInetAddress();
System.out.println("host ip:" + getHostIp(netAddress));
System.out.println("host name:" + getHostName(netAddress));
Properties properties = System.getProperties();
SetString set = properties.stringPropertyNames(); //获取java虚拟机和系统的信息。
for(String name : set){
System.out.println(name + ":" + properties.getProperty(name));
}
}
public static InetAddress getInetAddress(){
try{
return InetAddress.getLocalHost();
}catch(UnknownHostException e){
System.out.println("unknown host!");
}
return null;
}
public static String getHostIp(InetAddress netAddress){
if(null == netAddress){
return null;
}
String ip = netAddress.getHostAddress(); //get the ip address
return ip;
}
public static String getHostName(InetAddress netAddress){
if(null == netAddress){
return null;
}
String name = netAddress.getHostName(); //get the host address
return name;
}
}
这个代码简单明了,就是调用现成的InetAddress类
java中InetAddress的getAddress和getHostAddress有什么区别?
getHostAddress为byte数组,getAddress是个String字符串。
所以,getAddress方便展示,getHostAddress方便作为数据进行处理。
java中的getinetaddress和gethostaddress的区别
1. socket.getInetAddress()返回InetAddress对象包含远程计算机的IP地址。InetAddress.getHostAddress()返回String对象与该地址的文本表示。 因此,要结束与一个String您可以打印,这就是你如何做到这一点。

java如何获取网卡地址
看你获取的是win系统还是linux系统了:
java执行操作系统的网卡地址语句:
获取机器名:
[java] view plain copy
public String getLocalHostName() {
String hostName;
try {
InetAddress addr = InetAddress.getLocalHost();
hostName = addr.getHostName();
} catch (Exception ex) {
hostName = "";
}
return hostName;
}
获取IP(多个网卡时获取了多个IP):
[java] view plain copy
public ListString getNetworkAddress() {
ListString result = new ArrayListString();
EnumerationNetworkInterface netInterfaces;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
EnumerationInetAddress addresses=ni.getInetAddresses();
while(addresses.hasMoreElements()){
ip = addresses.nextElement();
if (!ip.isLoopbackAddress() ip.getHostAddress().indexOf(':') == -1) {
result.add(ip.getHostAddress());
}
}
}
return result;
} catch (Exception e) {
return null;
}
}
java中怎么查看本机回传地址
在 Java 中,可以通过以下代码获取本地地址:
```java
import java.net.InetAddress;
public class LocalHostAddress {
public static void main(String[] args) throws Exception {
InetAddress addr = InetAddress.getLocalHost();
System.out.println("Local HostAddress: "+addr.getHostAddress());
}
}
```
上述代码中,`getLocalHost()`方法返回一个`InetAddress`对象,该对象代表本地主机。使用`getHostAddress()`方法可以获取本地主机的 IP 地址。
需要注意的是,如果机器上有多个 IP 地址,`getHostAddress()`方法返回的可能不是你期望的 IP 地址,因此需要进行进一步处理,以获取正确的回传地址。
Java如何通过网络进行寻找附近的设备,附源码?
在 Java 中,可以通过使用 Java 的网络编程技术来实现查找附近的设备。具体的做法如下:
获取本机的 IP 地址和子网掩码,以计算出本机所在网络中的 IP 地址范围。
使用 for 循环和 InetAddress 类扫描网络中的每一个 IP 地址。
对于每一个 IP 地址,使用 isReachable() 方法判断其是否可达,如果可达则表明该 IP 地址对应的设备存在。
以下是一份简单的示例代码:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class FindDevices {
public static void main(String[] args) throws UnknownHostException {
InetAddress localHost = InetAddress.getLocalHost();
String hostAddress = localHost.getHostAddress();
String subnet = hostAddress.substring(0, hostAddress.lastIndexOf(".") + 1);
for (int i = 1; i 256; i++) {
String host = subnet + i;
try {
InetAddress address = InetAddress.getByName(host);
if (address.isReachable(1000)) {
System.out.println(host + " is reachable");
}
} catch (Exception e) {
System.out.println(host + " is not reachable");
}
}
}
}
请注意,这是一份示例代码,其中的扫描范围和扫描方法可能不是最佳的,根据实际需要进行修改。
关于javagethost和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
