且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android客户端和Java服务器的TCP通信

更新时间:2021-12-25 21:47:35

您与您的Andr​​oid客户端连接到本地主机

You connect with your Android client to localhost.

Socket s = new Socket("localhost", 8080);

当你的服务器是在Android设备上运行,这只会工作。但我认为这是您的PC上运行。所以,当你在Android模拟器上运行你的应用程序,你可以连接到10.0.2.2为了联系主机。在真实设备上,你必须找出哪些可以通过Android设备来达到您的服务器的IP地址,例如:通过Wi-Fi。

This would only work when your server is running on the Android device. But I think it is running on your PC. So, when you run your app on Android emulator, you can connect to 10.0.2.2 in order to contact the host. On a real device, you have to find out your servers IP address which can be reached by the Android device, e.g. via Wi-Fi.