-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameConfig.java
More file actions
50 lines (41 loc) · 1.27 KB
/
Copy pathGameConfig.java
File metadata and controls
50 lines (41 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package javaNetTest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class GameConfig {
public static String HostAddress;
public static String ServerUrl;
public static void main(String[] args) {
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
throw new Error(e);
}
// HostAddress = inetAddress.getHostAddress();
try {
Enumeration<NetworkInterface> netIf = NetworkInterface.getNetworkInterfaces();
while (netIf.hasMoreElements()) {
NetworkInterface nwi = netIf.nextElement();
Enumeration<InetAddress> address = nwi.getInetAddresses();
while (address.hasMoreElements()) {
InetAddress in = address.nextElement();
if (in.isSiteLocalAddress()) {
HostAddress = in.getHostAddress();
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
HostAddress = "unknown";
}
ServerUrl = "http://" + HostAddress + ":" + 9527;
// 有小米wifi时候 : http://192.168.123.1:9527
// 没有wifi时候 : http://10.0.0.132:9527
System.out.println(ServerUrl);
}
public void doSomething1() {
}
}