Android和Windows之间的Socket通信 [英] Socket communication between Android and Windows
问题描述
Android Emulator 是否可以作为套接字服务器,Windows 窗体应用程序将作为客户端,并且它们之间可以相互通信?
Is it possible to Android Emulator will be socket server and Windows form app will be client and they communicate each other?
我的模拟器 IP 地址是 192.168.232.2 但我无法从我的电脑 ping 并且我无法通过我的 winform 应用程序连接
My emulator IP address is 192.168.232.2 but i cannot ping from my pc and i cannot connect via my winform app
我能做什么?
推荐答案
在你的 Android 代码中创建 ServerSocket 时,你可以通过传递给它分配一个端口,例如这里我打开了一个 ServerSocket
8080 端口:
When creating a ServerSocket in your Android code, you can assign a port to it by passing it, for example here I'm opening a ServerSocket
on the port 8080:
val serverSocket = ServerSocket(8080)
然后你需要使用 adb
将传入的连接转发到你的模拟器,通过运行,这里再次转发端口 8080
You then need to use adb
to forward incoming connection to your emulator, by running, here forwarding the port 8080 again
adb forward tcp:8080 tcp:8080
此时,在端口 8080 上与您的 PC 的任何连接都将转发到您的模拟器.
At this point any connection to your PC on port 8080 will be forwarded to your emulator.
您可以通过在终端输入 ifconfig
或 ipconfig
来获取 IP,具体取决于您的操作系统.
You can get the IP by typing in the terminal ifconfig
or ipconfig
depending on your operating system.
测试它的最简单方法是在您的 PC 上同时运行客户端和服务器,并使用 localhost
或 127.0.0.1
作为 IP 地址.
The easiest way to test it is to run both client and server on your PC and just use localhost
or 127.0.0.1
as the IP address.
这篇关于Android和Windows之间的Socket通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!