我正在开发一个套接字应用。
背景:
我尝试过的事情:
我使用以下方法获取服务器IP地址,它为我提供了IP地址:
public String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += "Server running at : "
+ inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
text.setText(ip);
return ip;
}
我使用静态端口8080 [可能这是一个问题,如果有的话请建议我选择]。
我在客户端使用这些行进行连接,但是soket对象在不同的网络上返回null:
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
所以,请建议我应该用什么来进行沟通 不同的真实Android设备与不同的网络[应用程序运行相同 网络完美]?
答案 0 :(得分:0)
https://en.wikipedia.org/wiki/TCP_hole_punching
如果您已连接到P2P连接,则可以使用此技术。
否则,您可以获得两个客户端都可以访问的在线服务器,它可以用于在两者之间转发消息。亚马逊网络服务是一个很好的选择,你可以使用带有公共IP的T2.mini机器免费试用一年。