The Android Simulator communicates with the PC, the simulator and the simulation with the socket and obtains the simulator IP address program

Source: Internet
Author: User

To launch two emulators on this machine, the information for this machine (PC) and simulator is as follows:

As you can see, the IP address of the two simulator is exactly the same, so to realize the communication between the two simulators, using the emulator's IP address is impossible.

Get Emulator Name:

>ADB devices

List of devices attached

emulator-5554 Device

emulator-5556 Device

The simulator provides a special IP address of 10.0.2.2, this IP address can be said to be equivalent to the PC native IP address 127.0.0.1. Therefore, through this special IP address, the realization of PC native and simulator communication is no problem.

"Pc-server/Simulator-client"

Purpose: To communicate with the server via port 7100.

The implementation method is relatively simple, directly according to the following method is possible.

Method 1:

Pc-server:serversocket SERVER = new ServerSocket (7100);

Simulator-client:socket socket = new Socket ("192.168.1.3", 7100);

Method 2:

Pc-server:serversocket SERVER = new ServerSocket (7100);

Simulator-client:socket socket = new Socket ("10.0.2.2", 7100);

"Simulator-server/pc-client"

Purpose: Connect to port 7100 of the server.

The implementation is slightly more complex and cannot be done in the previous way. To achieve this, port mapping is performed first.

Map the native port tcp/6100 to the emulator tcp/7100 port (UDP is the same).

>adb–s emulator-5554 forward tcp:6100 tcp:7100

After the port mapping is successful, proceed to the following experiment.

Method 1 (correct):

Emulator-server:serversocket SERVER = new ServerSocket (7100);

Pc-client:socket socket = new Socket ("127.0.0.1", 6100);

Method 2 (Error):

Emulator-server:serversocket SERVER = new ServerSocket (7100);

Pc-client:socket socket = new Socket ("192.168.1.3", 6100);

The cause of the error is that 127.0.0.1 is a native loop IP that does not have a mapping relationship to the native IP, so the connection request sent to the native IP cannot be mapped to the 127.0.0.1 loop IP.

For this reason, the network connection is not possible between the simulators on the two PC computers. If you want to implement a connection, you can only run a background program for data relay on the PC that is the server and forward the data.

"Simulator (5554)-server/Simulator (5556)-client"

Purpose: The simulator (5556) is connected to the 7100 port of the simulator (5554).

Implementation is also relatively complex. To do this, port mapping is also done first.

Map the native port tcp/6100 to the emulator (5554) tcp/7100 port (UDP is the same).

>adb–s emulator-5554 forward tcp:6100 tcp:7100

After the port mapping is successful, proceed to the following experiment.

Method 1 (correct):

Simulator (5554)-server:serversocket SERVER = new ServerSocket (7100);

Simulator (5556)-client:socket socket = new Socket ("10.0.2.2", 6100);

Principle Description:

The simulator (5556) is mapped to 7100 ports of the simulator (5554) by connecting the 10.0.2.2:6100, which is equivalent to the 127.0.0.1:6100 connected to the native (PC), which is mapped to port 6100 of the native IP. Therefore, the connection request from the simulator (5556) is sent to the simulator (5554). The network communication function between the two simulators is realized.

This method is useful for native debugging.

Method 2 (Error):

Simulator (5554)-server:serversocket SERVER = new ServerSocket (7100);

Simulator (5556)-client:socket socket = new Socket ("192.168.1.3", 6100);

The cause of the error is the same as the "pc-client/Simulator-server" Method 2.

Transferred from: http://japankn.iteye.com/blog/1744031

The Android Simulator communicates with the PC, the simulator and the simulation with the socket and obtains the simulator IP address program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.