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)
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
At this point any connection to your PC on port 8080 will be forwarded to your emulator.
You can get the IP by typing in the terminal ifconfig
or ipconfig
depending on your operating system.
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…