The android project has recently used socket communication, and the communication between two simulators needs to be implemented using ADB. Here we will learn about ADB.
What is ADB:
ADB is called Android Debug Bridge, which serves as the debugging Bridge. With this tool, we can manage the status of devices or mobile phone simulators. In my personal opinion, you can view and manage Android devices through command lines on a pc, such as Android phones connected to your computer. It is more convenient to directly operate through the command line.
Common Operations:
(1) quickly update the code in a device or mobile phone simulator, such as an application or Android system upgrade;
(2) Run Shell commands on the device;
(3) Manage the ports on devices or mobile phone simulators;
(4) copy or paste the file on the device or mobile phone simulator.
For details, refer to the ADB tool for Android.
Because communication between two simulators is required, we start two simulators and view the status and IP port address of the two simulators through adb.
"Removed" file prompts that adb has been removed. Baidu found that the adb has been moved to the "platform-tools" directory in the new version, and the adb is found in the platform-tools directory, add the Path to the environment variable Path.
Then execute the adb command in the command line to identify the problem:
OK. Use ADB to view the current configurations of the simulator.
Start 2 simulators. The api versions are 10 (plat version 2.3.3). 5554 run socket server 5556 run socket Client
First, go to the command console of the emulator5554 device.
Enter the following command in the command line:
Adb-s emulator-5554 shell
Getprop
Find the following two lines in the result:
[Net. eth0.dns1]: [10.0.2.3]
[Net. gprs. local-ip]: [10.0.2.15]
Exit to exit shell
As shown above, if you continue to check the ip Address Configuration of emulator5556, you will find that the two are identical. How can we communicate?
The Android system sets the pc ip address to 10.0.2.2 for communication and 10.0.2.15/127.0.0.1 for itself. By default, all Android virtual machines can connect to the Internet through 10.0.2.2.
If you need to modify the dns, You can execute
setprop net.dns1 192.168.0.1
How can I modify the IP address? The two IP addresses are completely the same and cannot communicate with each other:
Data shows that it can be indirectly implemented through port ing to the Local Machine (refer to tcp/udp communication between PC Computer and Android simulator access and simulator)
1. Run the simulator
2. Open the window command line
5554 is the simulator port. After running the command, the system will go to the android console.
telnet 127.0.0.1 5554
Or
telnet localhost 5554
3. Run the following command on the console:
Redir add udp: Port Number
Or
Redir add tcp: Port Number
For example:
redir add tcp:8888:8888
The first port is the PC port and the second is the simulator port. (Port ing)
After executing this command, the data received by port PC8888 is forwarded to port 8888 of the simulator.
Redir: Port direction command
Add: add
Tcp/udp: indicates the port types tcp and udp
Communication code case reference:
Http://blog.163.com/fanning_7213/blog/static/2496505201137101721665/