之前,以为android模拟器只能作为client来对待,不能创建socket server来接收数据,原因是用tcpview看了一下,创建的监听端口,并没有显示,
所以,后来,就打算用vc直接开发一个daemon程序来接收请求,同时转发请求。
当apkserver完成后,发现adb命令支持端口转发功能,这才醒悟过来,白忙活了,不过,自己搞一个daemon还是可以的。
下面就是官方对adb的介绍,可以看看adb forward tcp:55555 tcp:5556命令。把发往本地监听55555端口的数据转发到模拟器里的监听端口5556.
adb来自官网的简介-Android Debug Bridge
Android Debug Bridge
ADB quickview
- Manage the state of an emulator or device
- Run shell commands on a device
- Manage port forwarding on an emulator or device
- Copy files to/from an emulator or device
In this document
- Issuing ADB Commands
- Querying for Emulator/Device Instances
- Directing Commands to a Specific Emulator/Device Instance
- Installing an Application
- Forwarding Ports
- Copying Files to or from an Emulator/Device Instance
- Listing of adb Commands
- Issuing Shell Commands
- Enabling logcat Logging
- Stopping the adb Server
See also
Android Debug Bridge (adb) is a versatile tool lets you manage thestate of an emulator instance or Android-powered device. It is aclient-server program that includes three components:
- A client, which runs on your development machine. You caninvoke a client from a shell by issuing an adb command. Other Androidtools such as the ADT plugin and DDMS also create adb clients.
- Aserver, which runs as a background process on your development machine.The server manages communication between the client and the adb daemonrunning on an emulator or device.
- A daemon, which runs as a background process on each emulator or device instance.
When you start an adb client, the client first checks whether thereis an adb server process already running. If there isn't, it starts theserver process. When the server starts, it binds to local TCP port 5037and listens for commands sent from adb clients—all adb clients use port5037 to communicate with the adb server.
The server then sets up connections to all running emulator/deviceinstances. It locates emulator/device instances by scanningodd-numbered ports in the range 5555 to 5585, the range used byemulators/devices. Where the server finds an adb daemon, it sets up aconnection to that port. Note that each emulator/device instanceacquires a pair of sequential ports — an even-numbered port for consoleconnections and an odd-numbered port for adb connections. For example:
Emulator 1, console: 5554
Emulator 1, adb: 5555
Emulator 2, console: 5556
Emulator 2, adb: 5557 ...
As shown, the emulator instance connected to adb on port 5555 is the same as the instance whose console listens on port 5554.
Once the server has set up connections to all emulator instances,you can use adb commands to control and access those instances. Becausethe server manages connections to emulator/device instances and handlescommands from multiple adb clients, you can control any emulator/deviceinstance from any client (or from a script).
The sections below describe the commands that you can use to accessadb capabilities and manage the state of an emulator/device. Note thatif you are developing Android applications in Eclipse and haveinstalled the ADT plugin, you do not need to access adb from thecommand line. The ADT plugin provides a trasparent integration of adbinto the Eclipse IDE. However, you can still use adb directly asnecessary, such as for debugging.
Issuing adb Commands
You can issue adb commands from a command line on your development machine or from a script. The usage is:
adb [-d|-e|-s <serialNumber>] <command>
When you issue a command, the program invokes an adb client. Theclient is not specifically associated with any emulator instance, so ifmultiple emulators/devices are running, you need to use the-d
option to specify the target instance to which the command should bedirected. For more information about using this option, seeDirecting Commands to a Specific Emulator/Device Instance.
Querying for Emulator/Device Instances
Before issuing adb commands, it is helpful to know whatemulator/device instances are connected to the adb server. You cangenerate a list of attached emulators/devices using thedevices
command:
adb devices
In response, adb prints this status information for each instance:
- Serial number — A string created by adb to uniquely identify an emulator/device instance by its console port number. The format of the serial number is
<type>-<consolePort>
. Here's an example serial number:emulator-5554
- Sta