One socket
The socket was originally used for Process Communication Between TCP/IP networks and in Client/Server mode.
Implement Asynchronous operations, share resources for centralized processing, and improve client response capabilities.
Basic TCP communication process:
Server Client
1. Create socket 1. Create socket
2. BIND ()
3. Listen ()
4. accecp ()
---- Waiting for client connection ---- 2. Connect ()
5. Read data (Recv) 3. Write Data (send)
6. Write Data (send) 4. Read data (Recv)
7. Disable socket (closesocket () 5. Disable socket (closesocket ())
Data Stream:
2. Android localsocket
Localsocket
Create a socket (non-server) in the Unix domain name space ).
Is to encapsulate the socket in Linux and call it in JNI mode to implement inter-process communication.
Specifically, the native layer Server communicates with the framework layer client, or the Client/Server mode can be used for communication at each layer.
Localserversocket
Create a Unix domain socket on the server, which corresponds to localsocket.
Localsocketimpl
Framework layer socket implementation, through the JNI call system Socket API.
Localsocketaddress
The address and space of the socket in the Unix domain.
JNI access interface:\ Frameworks \ base \ core \ JNI \ android_net_localsocketimpl.cpp
Socket_create
Socket_connect_local
Socket_bind_local
Socket_listen
......
Let's take a look at the relationships between these classes:
Using localsocket of Android to establish socket communication is consistent based on the network socket process.
Communication between three native and framework
Take install as an example:
1. Add socket Resources
\ System \ core \ rootdir \ init. RC:
Service installd/system/bin/installd
Class main
Socket installd stream 600 System
When the install service is started, socket file system resources are allocated to install:Dev/socket/Installd
The socket resource of the install service is bound with the name installd.
These are all completed when the service is started in the initialization INIT process:
Service_start
Create_socket
Publish_socket
2 native Layer
Install the process to create a serverProgram
The native layer serves as server: \ frameworks \ base \ cmds \ installd. c
Int Main ( Const Int Argc, Const Char *Argv []) { // Obtain the bound socket Lsocket = Android_get_control_socket (socket_path ); // Listen to socket Listen (lsocket, 5 ); For (;;){ // Wait for the client to establish a connection S = accept (lsocket, & ADDR ,& Alen ); For (;;){ // Receiving data is equivalent to Recv. Readx (S, Buf, count ); // Perform related operations Execute (S, Buf );} // Disable socket Close (s );}}
3 framework layer
Client Program:
\ Frameworks \ base \ Services \ Java \ com \ Android \ Server \ PM \ Installer. Java
Boolean connect (){ // Create socket Msocket = New Localsocket (); // Set connection address Localsocketaddress address = New Localsocketaddress ( " Installd " , Localsocketaddress. namespace. Reserved ); // Establish a connection Msocket. Connect (Address ); // Get data input stream readable data Min = Msocket. getinputstream (); // Data can be written to the data output stream. MOUT = Msocket. getoutputstream ();}
Therefore, the native layer service and framework are used to establish Client/Server mode socket communication.Code:
Main Code of the Java layer:
Localsocket S =Null; Localsocketaddress L; s=NewLocalsocket (); L=NewLocalsocketaddress (socket_name, localsocketaddress. namespace. Reserved); S. Connect (L );
Native layer main code:
S_fdlisten =Android_get_control_socket (socket_name); RET=Listen (s_fdlisten, n); s_fdcommand= Accept (s_fdlisten, (sockaddr *) & peeraddr, & socklen );
Add the following to init. RC:
Service myserver/system/bin/MyserverClassMain socket myserver stream600System ......
Of course, the establishment of this Client/Server mode does not have to be in the native layer and framework layer, but also in the framework layer.
The system provides localsocket as the client and localserversocket as the server.
ZygoteThe Service uses localserversocket as the server socket for communication.
Establish socket communication. You can also use localsocket and localserversocket during code execution.
Bind the socket resource created for the server in init. RC and during initialization, and use localserversocket in the code
The established server socket resources are different in Linux domain space, and the process is the same as that of General socket communication.
Iv. Establish socket communication between localsocket and localserversocket
Localsocket is a connection established on the server as a client to send data.
Localserversocket is used as the server to establish a socket to listen to client requests. You can see two ways through the constructor:
Create a new server socket in the Linux Abstract space:
PublicLocalserversocket (string name) throws ioexception {//Create a socket ResourceImpl =NewLocalsocketimpl (); impl. Create (True);//Binding addressLocaladdress =NewLocalsocketaddress (name); impl. BIND (localaddress );//ListenersImpl. Listen (listen_backlog );}
Use the file descriptor to create an existing and bound server socket:
For example, in init. RC, specify the socket resource DEV/socket /......, Zygote uses this method to create a socket as the server
Localserversocket socket = new localserversocket (Createfiledescriptor(Filedesc ));
PublicLocalserversocket (filedescriptor FD) throws ioexception {//Bound listenerImpl =NewLocalsocketimpl (FD); impl. Listen (listen_backlog); localaddress=Impl. getsockaddress ();}
Usually used:
Client code:
String message;//Create socketLocalsocket sender =NewLocalsocket ();//Establish a connection with the corresponding addressSender. Connect (NewLocalsocketaddress (socket_address ));//Send and write dataSender. getoutputstream (). Write (message. getbytes ());//Disable socketSender. getoutputstream (). Close ();
Server:
//Create a socket and bind the listener to the newly createdLocalserversocket Server =NewLocalserversocket (socket_address );While(True){//Waiting for connection EstablishmentLocalsocket explorer =Server. Accept ();//Receive and retrieve data streamsInputstream input =Aggreger. getinputstream ();
......}
Reference:
Http://hi.baidu.com/leoispace/item/ab4a271e6624824b6926bbe9
Http://www.cnblogs.com/over140/archive/2011/11/22/2258372.html
Http://blog.csdn.net/jamin0107/article/details/6832159