TCP/UDP communication between PC computer and Android simulator access and simulator

Source: Internet
Author: User
Tags socket port number
The Android system defaults to only a one-way access to the PC via IP (10.0.2.2), and PC computers cannot access the Android emulator system directly via IP. A port redirection (redir) is required to enable communication between PC and Android emulator systems and the Android emulator.
First, the Telnet client installation required for Port redirection:
Windows:
Install the Telnet client. If it is not installed, you can enable Telnet client functionality by locating the Telnet client menu item under Windows Program management by turning on or off system features.
Linux:
Install the Telnet client yourself.
First, PC computer can not directly access the Android simulator system reasons
The Android system sets the PC IP to 10.0.2.2 for communication, 10.0.2.15/127.0.0.1. PC computers, however, do not specify IP for the Android emulator system, so the PC can only communicate with the Android emulator via port redirection.
II. communication between PC computer and Android simulator system
1. Running simulator
2, open the Window command line, execute:
telnet localhost 5554
5554 is the port of the simulator (in the title bar of the Android Emulator window) and will go into the Android console after execution
3, under the console implementation:
Format: Redir add < udp/tcp >:< pc port >:< Simulator port >
Example: Redir add udp:2888:2888
Redir Add tcp:2888:2888
After this command is executed, the TCP/UDP data received by the PC 2888 Port is transferred to the 2888 port of the simulator.
Iii. communication between multiple Android simulator systems
1. Start simulator emulator-5554 and emulator-5556
2, open the DOS window to perform telnet localhost 5554 connect to the simulator 5554
3. After successful connection, continue execution: Redir add tcp:5000:6000 bind PC Port 5000 to port 6000 on emulator 5554.
4, at this time simulator 5556 by the PC computer Port 5000 (that is, Address: 10.0.2.2:5000) to send TCP/UDP packets and simulator 5554 communication.
5, similarly according to step 2, 3来 realizes the PC computer to the simulator 5556 port forwarding.
After adding success, we can use the redir List command to list the mapped ports that have been added, redir del can be deleted.
It is believed that if you understand the communication between PC and Android simulator system , you will know how to implement communication between multiple simulators.
    • TCP Communication
When TCP communication is in progress, Two simulators are started on this machine, and the local (PC) and simulator are found the IP address of two simulator is identical, so to realize communication between two simulators, it is impossible to use the IP address of the simulator. Port mapping is required.
Simulator provides a special IP, this IP address is 10.0.2.2, this IP address can be said to be equivalent to the PC native IP address 127.0.0.1. Therefore, it is no problem to realize the communication between PC native and simulator through this special IP address.
First, run the emulator at the command line adb–s emulator-Emulator forward TCP: Port number TCP: Port number (for example:adb–s emulator-5554 forward tcp:8080 t cp:8090)
Pc-server: serversocket server = new ServerSocket (8080);
simulator-client: Socket socket = new Socket ("10.0.2.2", 8090);
The next step is how to communicate between two emulators: the same advanced Line port mapping:adb–s Emulator-Emulator forward TCP: Port number TCP: Port number (first run emulator). The code is as follows:
Adb–s emulator-5554 forward tcp:8080 tcp:8081
Simulator (5554)-server: serversocket server = new ServerSocket (8080);
Simulator (5556)-client: Socket socket = new Socket ("10.0.2.2", 8081);
because emulator 5556-connected address 10.0.2.2 : 8080 corresponds to the 127.0.0.1:8080 of this machine, because the connection request on the port map native IP8080 is mapped to 5,554:8,081, so the request of emulator 5556 is sent to 5554.
Related Article

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.