Home » Android » ADB communication protocol analysis and Implementa...

ADB communication protocol analysis and Implementation (three) (Adb network communication part resolution)

this article says the socket protocol for communication between ADB and the ADB service process.

After the adb service startup process, will monitor the local port 5037, using the TCP protocol, so ADB sends command is to send data local port 5037, as network programming knows, this machine can use the IP address. The IP address and port number of the socket communication is no problem

1) command send mode

The command is sent as a byte stream. The string needs to be encoded into utf8.. A command needs to send two parts. First send the command length and then send the command itself. For example, "ABCDEFGHIJKLMN"

1. first send the command length.

int iLen = strlen (` ABCDEFGHIJKLMN ');

char tmp[5];

sprintf (TMP,%04x, iLen);

SendData (tmp, 4); / / function to send data, TMP cache address, 4 said 4 bytes

2. sends the command itself,

SendData ("ABCDEFGHIJKLMN", iLen);

After the 3. command is sent, the server's data needs to be received. Normally, the server will return a 4 byte status,.OKAY, indicating the successful processing of

char, tmp[5];

int, iRead = ReadData ((byte*) TMP, 4);

if (0 = = memcmp (TMP, OKAY, 4))


// receive successful




// failed,


above is very simple commands. Some complex command processing methods, such as installing APK, it needs to do more things. The file transfer to the mobile phone, then send the installation command. These complex command is only command format more. According to ADB's source code can be written..

here, roughly speaking,

1) send a sync command with the destination path to copy the document

2) send the contents of the file

3) send a shell:pm command to start installation

The detail implementation section can be QQ:2506314894