java版服务器地址怎么看

Python016

java版服务器地址怎么看,第1张

方法如下

1、单击电脑桌面“win”图标,在菜单中点击设置选项,进入设置页面

2、点击“网络和Internet”选项,接着点击左侧属性栏的“状态”选项;

3、在页面中进入“网络和共享中心”页面,在页面中单击连接的网络名称;

4、在弹窗中选择“常规”选项,然后点击页面中的“详细信息”选项;

5、在详细信息页面即可查看当前服务器地址

以windows举例。\x0d\x0a运行命令" cmd ipconfig /all"就会出现以下结果\x0d\x0a \x0d\x0aPhysical Address. . . . . . . . . : 20-CF-30-9A-60-EE\x0d\x0a。\x0d\x0ajava就能过这样的命令来获取。以下是示例。\x0d\x0a\x0d\x0aimport java.io.BufferedReader\x0d\x0aimport java.io.IOException\x0d\x0aimport java.io.InputStreamReader\x0d\x0a\x0d\x0apublic class TestMac\x0d\x0a{\x0d\x0a public static void main(String[] args) {\x0d\x0aSystem.out.println("Operation System=" + getOsName())\x0d\x0aSystem.out.println("Mac Address=" + getMACAddress())\x0d\x0aSystem.out.println("通过ip获取mac"+getMACAddress("192.168.1.101"))\x0d\x0a }\x0d\x0a\x0d\x0apublic static String getOsName() {\x0d\x0aString os = ""\x0d\x0aos = System.getProperty("os.name")\x0d\x0areturn os\x0d\x0a}\x0d\x0a\x0d\x0apublic static String getMACAddress() {\x0d\x0aString address = ""\x0d\x0aString os = getOsName()\x0d\x0aif (os.startsWith("Windows")) {\x0d\x0atry {\x0d\x0aString command = "cmd.exe /c ipconfig /all"\x0d\x0aProcess p = Runtime.getRuntime().exec(command)\x0d\x0aBufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))\x0d\x0aString line\x0d\x0awhile ((line = br.readLine()) != null) {\x0d\x0aif (line.indexOf("Physical Address") >0) {\x0d\x0aint index = line.indexOf(":")\x0d\x0aindex += 2\x0d\x0aaddress = line.substring(index)\x0d\x0abreak\x0d\x0a}\x0d\x0a}\x0d\x0abr.close()\x0d\x0areturn address.trim()\x0d\x0a} catch (IOException e) {\x0d\x0a}\x0d\x0a} else if (os.startsWith("Linux")) {\x0d\x0aString command = "/bin/sh -c ifconfig -a"\x0d\x0aProcess p\x0d\x0atry {\x0d\x0ap = Runtime.getRuntime().exec(command)\x0d\x0aBufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))\x0d\x0aString line\x0d\x0awhile ((line = br.readLine()) != null) {\x0d\x0aif (line.indexOf("HWaddr") >0) {\x0d\x0aint index = line.indexOf("HWaddr") + "HWaddr".length()\x0d\x0aaddress = line.substring(index)\x0d\x0abreak\x0d\x0a}\x0d\x0a}\x0d\x0abr.close()\x0d\x0a} catch (IOException e) {\x0d\x0a}\x0d\x0a}\x0d\x0aaddress = address.trim()\x0d\x0areturn address\x0d\x0a}\x0d\x0a\x0d\x0apublic static String getMACAddress(String ipAddress) { \x0d\x0aString str = "", strMAC = "", macAddress = ""\x0d\x0atry { \x0d\x0aProcess pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress)\x0d\x0aInputStreamReader ir = new InputStreamReader(pp.getInputStream())\x0d\x0aLineNumberReader input = new LineNumberReader(ir)\x0d\x0afor (int i = 1i str = input.readLine()\x0d\x0aif (str != null) { \x0d\x0aif (str.indexOf("MAC Address") >1) { \x0d\x0astrMAC = str.substring(str.indexOf("MAC Address") + 14, \x0d\x0astr.length())\x0d\x0abreak\x0d\x0a} \x0d\x0a} \x0d\x0a} \x0d\x0a} catch (IOException ex) { \x0d\x0areturn "Can't Get MAC Address!"\x0d\x0a} \x0d\x0a// \x0d\x0aif (strMAC.length() return "Error!"\x0d\x0a} \x0d\x0a\x0d\x0amacAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5) \x0d\x0a+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11) \x0d\x0a+ ":" + strMAC.substring(12, 14) + ":" \x0d\x0a+ strMAC.substring(15, 17)\x0d\x0a// \x0d\x0areturn macAddress\x0d\x0a} \x0d\x0a} \x0d\x0a\x0d\x0a剑天梦的回答原理和我这个一样,都是通过Process 执行命令。 我直接补充到答案里了。不过\x0d\x0a我这边运行那个命令出来的结果很多,那么花的时间就长了。优点是能够获取别人的mac地址 。

在java中内存中的对象地址是可变的,所以获得的内存地址有可能会变化。要获得内存地址也只能通过Unsafe的方法来获得,如下代码示例:

package com.bijian.study

import java.lang.reflect.Field

import sun.misc.Unsafe

public class Addresser {

//实例化Unsafe 类

private static Unsafe unsafe

static {

try {

//得到field对象

Field field = Unsafe.class.getDeclaredField("theUnsafe")

//设置获取地址

field.setAccessible(true)

unsafe = (Unsafe) field.get(null)

} catch (Exception e) {

e.printStackTrace()

}

}

public static long addressOf(Object o) throws Exception {

Object[] array = new Object[] { o }

long baseOffset = unsafe.arrayBaseOffset(Object[].class)

int addressSize = unsafe.addressSize()

long objectAddress

switch (addressSize) {

case 4:

objectAddress = unsafe.getInt(array, baseOffset)

break

case 8:

objectAddress = unsafe.getLong(array, baseOffset)

break

default:

throw new Error("unsupported address size: " + addressSize)

}

return (objectAddress)

}

//打印地址的长度

public static void main(String... args) throws Exception {

Object mine = "Hi there".toCharArray()

long address = addressOf(mine)

System.out.println("Addess: " + address)

// Verify address works - should see the characters in the array in the output

printBytes(address, 27)

}

//调用此方法得到地址

public static void printBytes(long objectAddress, int num) {

//循环打印得到的地址。

for (long i = 0 i < num i++) {

int cur = unsafe.getByte(objectAddress + i)

System.out.print((char) cur)

}

System.out.println()

}

}

运行结果: