Android IP地址与java
我正在编写一款支持多人游戏的 Android 视频游戏。有一个正在运行的专用服务器,当通过打开套接字单击多人游戏按钮时,机器人会连接到该服务器(这工作正常)。服务器基本上只是充当匹配系统。
当客户端托管游戏时,服务器会将该客户端添加到主机列表中。其他客户端可以选择查看此列表,然后连接到该主机。这就是问题所在。服务器应该跟踪主机的 IP/端口,然后其他客户端应该使用此信息打开与主机的套接字,然后游戏开始。我试图让主机将其自己的 IP 地址发送到服务器以供其他客户端稍后使用。
到目前为止我已经尝试了很多方法。一是:
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
}
这会返回10.0.2.15,这对于其他客户端来说显然是无用的。
我尝试过的另一种方法是这样的:
String hostName = InetAddress.getLocalHost().getHostName();
InetAddress addrs[] = InetAddress.getAllByName(hostName);
for (InetAddress addr: addrs) {
System.out.println ("addr.getHostAddress() = " + addr.getHostAddress());
System.out.println ("addr.getHostName() = " + addr.getHostName());
System.out.println ("addr.isAnyLocalAddress() = " + addr.isAnyLocalAddress());
System.out.println ("addr.isLinkLocalAddress() = " + addr.isLinkLocalAddress());
System.out.println ("addr.isLoopbackAddress() = " + addr.isLoopbackAddress());
System.out.println ("addr.isMulticastAddress() = " + addr.isMulticastAddress());
System.out.println ("addr.isSiteLocalAddress() = " + addr.isSiteLocalAddress());
System.out.println ("");
if (!addr.isLoopbackAddress()){// && addr.isSiteLocalAddress()) {
myIP = addr.getHostAddress();
}
当我将它作为java应用程序运行时,它会返回我正在寻找的ip地址,但是当我将它作为android应用程序运行时,它不起作用。最后一个 if 条件不知何故不满足, myIP 最终为空。请注意,我已包含以下权限:android.permission.INTERNET、android.permission.ACCESS_WIFI_STATE、android.permission.ACCESS_COARSE_LOCATION、android.permission.ACCESS_NETWORK_STATE。
有人可以帮助我吗?
I'm writing an Android video game that supports multiplayer. There is a dedicated server running which the androids connect to when the multiplayer button is clicked by opening a socket(this works fine). The server basically just acts as a matchmaking system.
When a client hosts a game, the server adds that client to the list of hosts. Other clients may choose to view this list and then subsequently connect to that host. This is where the problem is. The server is supposed to keep track of the ip/port of hosts, and then other clients are supposed to use this information to open a socket with the host and then the game starts. I'm trying to get the host to send its own IP address to server for other clients to use later.
I have tried many methods so far. One is:
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
}
This returns 10.0.2.15, which is obviously useless for other clients.
The other method I've tried is this:
String hostName = InetAddress.getLocalHost().getHostName();
InetAddress addrs[] = InetAddress.getAllByName(hostName);
for (InetAddress addr: addrs) {
System.out.println ("addr.getHostAddress() = " + addr.getHostAddress());
System.out.println ("addr.getHostName() = " + addr.getHostName());
System.out.println ("addr.isAnyLocalAddress() = " + addr.isAnyLocalAddress());
System.out.println ("addr.isLinkLocalAddress() = " + addr.isLinkLocalAddress());
System.out.println ("addr.isLoopbackAddress() = " + addr.isLoopbackAddress());
System.out.println ("addr.isMulticastAddress() = " + addr.isMulticastAddress());
System.out.println ("addr.isSiteLocalAddress() = " + addr.isSiteLocalAddress());
System.out.println ("");
if (!addr.isLoopbackAddress()){// && addr.isSiteLocalAddress()) {
myIP = addr.getHostAddress();
}
This returns the ip address that I'm looking for when I run it as a java application, but when I run it as an android application, it doesn't work. The last if condition is somehow not satisfied and myIP ends up being null. Note that I have included the permissions: android.permission.INTERNET, android.permission.ACCESS_WIFI_STATE, android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_NETWORK_STATE.
Can anybody help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只需要 Wifi 连接的 IP,您可以将 IP 作为 32 位整数检索:
然后,为了以点十进制表示法构造 IP;位移位并屏蔽结果:
清单中需要 android.permission.ACCESS_WIFI_STATE 权限。
If you just need the IP for the Wifi connection you can retrieve the IP as a 32 bit integer:
Then, in order to construct the IP in dot-decimal notation; bit-shift and mask the result:
android.permission.ACCESS_WIFI_STATE permission will be required in the manifest.
您是否必须依靠主机来计算自己的 IP 地址并将其提供给服务器?如果主机打开连接并向服务器发送一条消息,宣布它正在托管游戏,那么服务器是否可以使用连接和消息来自的 IP 地址?这将完全避免这个问题。
Do you have to rely on the host to figure out its own IP address and provide this to the server? If the host opens a connection and sends a message to the server announcing that it is hosting a game, then could the server use the IP address that the connection and message came from? This would avoid the problem altogether.
试试这个
try this