在 Andorid进程间通信之 UNIX SOCKET 一文的实战中,服务端和客户端的代码都是用C语言写的,而Androidd的系统源代码以及APP大多数都是java写的,那么不同的语言写的进程之间,能不能互相通信呢?答案是可以的。本文分为两部分
1,C代码进程做服务端,Android APP做客户端
对于Andorid APP ,做客户端的话,通过LocalSocket来实现。具体代码如下
LocalSocket lsocket = new LocalSocket();
LocalSocketAddress address = new LocalSocketAddress("/dev/server-socket", LocalSocketAddress.Namespace.ABSTRACT);
lsocket.connect(address);
在C代码中,作为客户端的话,会调用 socket和connect 来和服务端建立连接。java代码也是一样的,在connect方法中也是调用socket和connect 来和服务端建立连接。
需要注意的是,address的Namespace默认为ABSTRACT,在调用connect的过程中,对于ABSTRACT,有以下处理
int socket_make_sockaddr_un(const char *name, int namespaceId,
struct sockaddr_un *p_addr, socklen_t *alen)
{
memset (p_addr, 0, sizeof (*p_addr));
size_t namelen;
switch (namespaceId) {
case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
#if defined(__linux__)
namelen = strlen(name);
p_addr->sun_path[0] = 0;//1
memcpy(p_addr->sun_path + 1, name, namelen);
#else
//省略
#endif
//省略
p_addr->sun_family = AF_LOCAL;
*alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;//2
break;
注释1处,将p_addr的 sun_path[0]设为0 。注释2处因为注释1处的赋值,所以需要对地址的长度进行重新计算。所以在C代码的服务端也需要做对应的修改才行,修改如下:
my_addr.sun_path[0] = 0;
strcpy(my_addr.sun_path+1, MY_SOCK_PATH);
socklen_t len = sizeof(my_addr.sun_family)+strlen(MY_SOCK_PATH) + 1;
result = connect(socket_fd,(struct sockaddr*)&my_addr,len);
其它都不用修改,两端就能正常进行通讯了。
java完整代码示例
private void socketmessage() {
try {
LocalSocket lsocket = new LocalSocket();
LocalSocketAddress address = new LocalSocketAddress("/dev/server-socket", LocalSocketAddress.Namespace.ABSTRACT);
lsocket.connect(address);
String result;
/*向服务端写入数据*/
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(lsocket.getOutputStream()));
br.write("client say hello!");
br.newLine();
br.flush();
/*读取数据*/
InputStream inputStream = lsocket.getInputStream();
byte[] buf = new byte[inputStream.available() -1];
inputStream.read(buf,0,inputStream.available() -1) ;
String str =new String(buf);
Log.d("test","str:"+str);
lsocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
C代码参考文头提的那篇文章即可,只需记得做上面对地址处理的修改。
2,C代码进程做客户端,Android APP做服务端
同理,C代码做客户端的话,在绑定的时候也仅仅需要对地址额外的处理一下就行了,代码如下:
my_addr.sun_path[0] = 0;
strcpy(my_addr.sun_path+1, MY_SOCK_PATH);
socklen_t len = sizeof(my_addr.sun_family)+strlen(MY_SOCK_PATH) + 1;
unlink(MY_SOCK_PATH);
result = bind(listenfd,(struct sockaddr *)&my_addr,len);
这里重点说一下java做服务端,是如何实现的。参考Zygote
做服务端的话,通过LocalServerSocket来实现。第一步就是创建一个LocalServerSocket对象
registerZygoteSocket("/dev/server-socket");
private void registerZygoteSocket(String socketName) {
if (sServerSocket == null) {
try {
sServerSocket = new LocalServerSocket(socketName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
在新建一个LocalServerSocket对象时,会依次调用socket,bind,listen 来创建socket并监听服务端连接的请求。
第二部就是进入循环,在循环中等待客户端请求连接。在循环中使用epoll机制。连接了就可以和客户端相互通信了
private void runSelectLoop(){
ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>();
ArrayList<LocalSocketClass> peers = new ArrayList<LocalSocketClass>();
peers.add(null);//占一个位置
fds.add(sServerSocket.getFileDescriptor());
while (true){
StructPollfd[] pollFds = new StructPollfd[fds.size()];
for (int i = 0; i < pollFds.length; ++i) {
pollFds[i] = new StructPollfd();
pollFds[i].fd = fds.get(i);
pollFds[i].events = (short) POLLIN;
}
try {
Os.poll(pollFds, -1);
} catch (ErrnoException e) {
e.printStackTrace();
}
for (int i = pollFds.length - 1; i >= 0; --i) {
if ((pollFds[i].revents & POLLIN) == 0) {
continue;
}
if (i == 0) {//有客户端建立连接,因为fds数组中第一个元素是服务端的socket fd
try {
LocalSocket localSocket = sServerSocket.accept();
LocalSocketClass localSocketClass = new LocalSocketClass(localSocket);
peers.add(localSocketClass);
fds.add(localSocket.getFileDescriptor());
} catch (IOException e) {
e.printStackTrace();
}
} else {//客户端有数据来了(其它的fd都是通过accept得到的客户端的fd)
boolean done = peers.get(i).runOnce();
if (done) {
peers.remove(i);
fds.remove(i);
}
}
}
}
}
runOnce中就是对数据的处理
public boolean runOnce(){
/*读取客户端数据*/
try {
BufferedReader mSocketReader = new BufferedReader(
new InputStreamReader(localSocket.getInputStream()), 256);
String s = mSocketReader.readLine();
Log.d("test", "runOnce: "+s);
/*向服务端写入数据*/
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(localSocket.getOutputStream()));
br.write(s);
br.newLine();
br.flush();
}catch (Exception e){
e.printStackTrace();
}
return false;
}
总结
对于UNIX SOCKET ,C代码和Java代码之间是可以相互通信的,并且可以相互作为客户端和服务端通信。有一点需要注意的是:两端对于地址的处理一定要保持一致,否则两端连接不了。