Android本地服务器套接字

11

在Android中,有两个类LocalServerSocket和LocalSocket。我认为它们类似于Unix Socket中的AF_LOCAL(我不确定是否正确)。

我的问题是: 是否可以在Java中创建LocalServerSocket,并使用普通的Unix Socket客户端连接到本地或其他进程中的LocalServerSocket? 如果可能,我应该在本机中设置什么“sockaddr_un.sun_path”?

我编写了一个简单的测试项目,尝试将.sun_path设置为与LocalServerSocket中使用的字符串名称相同,但失败了,本机无法连接到Java LocalServerSocket。

我的Java代码:

package test.socket;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class TestSocketActivity extends Activity {

    public static String SOCKET_ADDRESS = "my.local.socket.address";
    public String TAG = "Socket_Test";


    static{System.loadLibrary("testSocket");}
    private native void clientSocketThreadNative();
    private native void setStopThreadNative();
    localServerSocket mLocalServerSocket;
    localClientSocket mLocalClientSocket;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mLocalServerSocket = new localServerSocket();

        mLocalClientSocket = new localClientSocket();
    }

    /* LocalServerSocket */
    public class localServerSocket extends Thread {

        int bufferSize = 32;
        byte[] buffer;
        int bytesRead;
        int totalBytesRead;
        int posOffset;
        LocalServerSocket server;
        LocalSocket receiver;
        InputStream input;
        private volatile boolean stopThread;

        public localServerSocket() {
            Log.d(TAG, " +++ Begin of localServerSocket() +++ ");
            buffer = new byte[bufferSize];
            bytesRead = 0;
            totalBytesRead = 0;
            posOffset = 0;

            try {
                server = new LocalServerSocket(SOCKET_ADDRESS);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.d(TAG, "The LocalServerSocket created failed !!!");
                e.printStackTrace();
            }

            stopThread = false;
        }

        public void run() {             
            Log.d(TAG, " +++ Begin of run() +++ ");
                while (!stopThread) {

                    if (null == server){
                        Log.d(TAG, "The LocalServerSocket is NULL !!!");
                        stopThread = true;
                        break;
                    }

                    try {
                        Log.d(TAG, "LocalServerSocket begins to accept()");
                        receiver = server.accept();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        Log.d(TAG, "LocalServerSocket accept() failed !!!");
                        e.printStackTrace();
                        continue;
                    }                   

                    try {
                        input = receiver.getInputStream();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        Log.d(TAG, "getInputStream() failed !!!");
                        e.printStackTrace();
                        continue;
                    }

                    Log.d(TAG, "The client connect to LocalServerSocket");

                    while (receiver != null) {

                        try {
                            bytesRead = input.read(buffer, posOffset,
                                    (bufferSize - totalBytesRead));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            Log.d(TAG, "There is an exception when reading socket");
                            e.printStackTrace();
                            break;
                        }

                        if (bytesRead >= 0) {
                            Log.d(TAG, "Receive data from socket, bytesRead = "
                                    + bytesRead);
                            posOffset += bytesRead;
                            totalBytesRead += bytesRead;
                        }

                        if (totalBytesRead == bufferSize) {
                            Log.d(TAG, "The buffer is full !!!");
                            String str = new String(buffer);
                            Log.d(TAG, "The context of buffer is : " + str);

                            bytesRead = 0;
                            totalBytesRead = 0;
                            posOffset = 0;
                        }

                    }
                    Log.d(TAG, "The client socket is NULL !!!");
                }
                Log.d(TAG, "The LocalSocketServer thread is going to stop !!!");
                if (receiver != null){
                    try {
                        receiver.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if (server != null){
                    try {
                        server.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
        }

        public void setStopThread(boolean value){
            stopThread = value;
            Thread.currentThread().interrupt(); // TODO : Check
        }

    }




    /* Client native socket */
    public class localClientSocket extends Thread {

        private volatile boolean stopThread;

        public localClientSocket(){
            Log.d(TAG, " +++ Begin of localClientSocket() +++ ");
            stopThread = false;
        }

        public void run(){
            Log.d(TAG, " +++ Begin of run() +++ ");
            while(!stopThread){
                clientSocketThreadNative();
            }
        }

        public void setStopThread(boolean value){
            stopThread = value;
            setStopThreadNative();
            Thread.currentThread().interrupt(); // TODO : Check
        }
    }


    public void bt_startServerOnClick(View v) {
        mLocalServerSocket.start();
    }

    public void bt_startClientOnClick(View v) {
        mLocalClientSocket.start();
    }

    public void bt_stopOnClick(View v) {
        mLocalClientSocket.setStopThread(true);
        mLocalServerSocket.setStopThread(true);
    }

}

我的本地代码:

#define SOCKET_NAME "my.local.socket.address"

JNIEXPORT void JNICALL Java_test_socket_TestSocketActivity_clientSocketThreadNative
  (JNIEnv *env, jobject object){

    LOGD("In clientSocketThreadNative() : Begin");

    stopThread = 1;

    int sk, result;
    int count = 1;
    int err;

    char *buffer = malloc(8);

    int i;
    for(i = 0; i<8; i++){
        buffer[i] = (i+1);
    }

    /*
    struct sockaddr_un addr;

    bzero((char *)&addr,sizeof(addr);
    addr.sun_family = AF_UNIX;
    addr.sun_path = SOCKET_NAME;
    */

    struct sockaddr_un addr = {
        AF_UNIX, SOCKET_NAME
    };

    LOGD("In clientSocketThreadNative() : Before creating socket");
    sk = socket(PF_LOCAL, SOCK_STREAM, 0);

    if (sk < 0) {
        err = errno;
        LOGD("%s: Cannot open socket: %s (%d)\n",
            __FUNCTION__, strerror(err), err);
        errno = err;
        return;
    }

    LOGD("In clientSocketThreadNative() : Before connecting to Java LocalSocketServer");
    if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
        err = errno;
        LOGD("%s: connect() failed: %s (%d)\n",
            __FUNCTION__, strerror(err), err);
        close(sk);
        errno = err;
        return;
    }

    LOGD("In clientSocketThreadNative() : Connecting to Java LocalSocketServer succeed");

    while(!stopThread){
        result = write(sk, buffer, 8);
        LOGD("In clientSocketThreadNative() : Total write = %d", result);
        count++;
        if(4 == count){
            sleep(1);
            count = 0;
        } 
    }

    LOGD("In clientSocketThreadNative() : End");
}
任何建议都将不胜感激 !!!

我再次阅读了开发者网站,发现我的做法可能注定要失败。因为LocalServerSocket使用Linux抽象命名空间而不是文件系统上的内容,本机客户端套接字将无法找到要连接的服务器。不过这只是我的理解...请问是否有任何建议? - Bohan Lu
我的先前评论可能不正确!根据Android文档,LocalServerSocket的默认命名空间是ABSTRACT。如果系统定义了"HAVE_LINUX_LOCAL_SOCKET_NAMESPACE",则“0”将添加到sun_path的开头。最终的sun_path将是"0" + "原始名称"。此部分可在源代码/system/core/libcutils/socket_local_server.c中找到。然而,即使我在本地代码中将“0”添加到我的sun_path中,本地客户端仍无法连接到Java LocalServerSocket! - Bohan Lu
4个回答

12

以下代码可能不完美,但它能够正常工作!感谢Mike。

Java部分(Socket服务器):

package test.socket;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class TestSocketActivity extends Activity {

    public static String SOCKET_ADDRESS = "/test/socket/localServer";
    public String TAG = "Socket_Test";


    static{System.loadLibrary("testSocket");}
    private native void clientSocketThreadNative();
    private native void setStopThreadNative();
    localSocketServer mLocalSocketServer;
    localSocketClient mLocalSocketClient;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mLocalSocketServer = new localSocketServer();

        mLocalSocketClient = new localSocketClient();
    }

    /* LocalSocketServer */
    public class localSocketServer extends Thread {

        int bufferSize = 32;
        byte[] buffer;
        int bytesRead;
        int totalBytesRead;
        int posOffset;
        LocalServerSocket server;
        LocalSocket receiver;
        InputStream input;
        private volatile boolean stopThread;

        public localSocketServer() {
            Log.d(TAG, " +++ Begin of localSocketServer() +++ ");
            buffer = new byte[bufferSize];
            bytesRead = 0;
            totalBytesRead = 0;
            posOffset = 0;

            try {
                server = new LocalServerSocket(SOCKET_ADDRESS);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.d(TAG, "The localSocketServer created failed !!!");
                e.printStackTrace();
            }

             LocalSocketAddress localSocketAddress; 
             localSocketAddress = server.getLocalSocketAddress();
             String str = localSocketAddress.getName();

             Log.d(TAG, "The LocalSocketAddress = " + str);

            stopThread = false;
        }

        public void run() {             
            Log.d(TAG, " +++ Begin of run() +++ ");
                while (!stopThread) {

                    if (null == server){
                        Log.d(TAG, "The localSocketServer is NULL !!!");
                        stopThread = true;
                        break;
                    }

                    try {
                        Log.d(TAG, "localSocketServer begins to accept()");
                        receiver = server.accept();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        Log.d(TAG, "localSocketServer accept() failed !!!");
                        e.printStackTrace();
                        continue;
                    }                   

                    try {
                        input = receiver.getInputStream();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        Log.d(TAG, "getInputStream() failed !!!");
                        e.printStackTrace();
                        continue;
                    }

                    Log.d(TAG, "The client connect to LocalServerSocket");

                    while (receiver != null) {

                        try {
                            bytesRead = input.read(buffer, posOffset,
                                    (bufferSize - totalBytesRead));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            Log.d(TAG, "There is an exception when reading socket");
                            e.printStackTrace();
                            break;
                        }

                        if (bytesRead >= 0) {
                            Log.d(TAG, "Receive data from socket, bytesRead = "
                                    + bytesRead);
                            posOffset += bytesRead;
                            totalBytesRead += bytesRead;
                        }

                        if (totalBytesRead == bufferSize) {
                            Log.d(TAG, "The buffer is full !!!");
                            String str = new String(buffer);
                            Log.d(TAG, "The context of buffer is : " + str);

                            bytesRead = 0;
                            totalBytesRead = 0;
                            posOffset = 0;
                        }

                    }
                    Log.d(TAG, "The client socket is NULL !!!");
                }
                Log.d(TAG, "The LocalSocketServer thread is going to stop !!!");
                if (receiver != null){
                    try {
                        receiver.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if (server != null){
                    try {
                        server.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
        }

        public void setStopThread(boolean value){
            stopThread = value;
            Thread.currentThread().interrupt(); // TODO : Check
        }

    }

    /* Client native socket */
    public class localSocketClient extends Thread {

        private volatile boolean stopThread;

        public localSocketClient(){
            Log.d(TAG, " +++ Begin of localSocketClient() +++ ");
            stopThread = false;
        }

        public void run(){
            Log.d(TAG, " +++ Begin of run() +++ ");
            while(!stopThread){
                clientSocketThreadNative();
            }
        }

        public void setStopThread(boolean value){
            stopThread = value;
            setStopThreadNative();
            Thread.currentThread().interrupt(); // TODO : Check
        }
    }


    public void bt_startServerOnClick(View v) {
        mLocalSocketServer.start();
    }

    public void bt_startClientOnClick(View v) {
        mLocalSocketClient.start();
    }

    public void bt_stopOnClick(View v) {
        mLocalSocketClient.setStopThread(true);
        mLocalSocketServer.setStopThread(true);
    }

}

本地 C 代码部分(客户端)

#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/un.h>


#include "test_socket_TestSocketActivity.h"

#define LOCAL_SOCKET_SERVER_NAME "/test/socket/localServer"

volatile int stopThread;

#ifndef __JNILOGGER_H_
#define __JNILOGGER_H_ 
#include <android/log.h> 
#ifdef __cplusplus
extern "C" {
#endif
#ifndef LOG_TAG
#define LOG_TAG "NativeSocket"
#endif
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,LOG_TAG,__VA_ARGS__)
#define LOGS(...) __android_log_print(ANDROID_LOG_SILENT,LOG_TAG,__VA_ARGS__)
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE,LOG_TAG,__VA_ARGS__)
#ifdef __cplusplus
}
#endif
#endif /* __JNILOGGER_H_ */ 


JNIEXPORT void JNICALL Java_test_socket_TestSocketActivity_clientSocketThreadNative
  (JNIEnv *env, jobject object){

    LOGD("In clientSocketThreadNative() : Begin");

    stopThread = 0;

    int sk, result;
    int count = 1;
    int err;

    char *buffer = malloc(8);

    int i;
    for(i = 0; i<8; i++){
        buffer[i] = (i+1);
    }

    struct sockaddr_un addr;
    socklen_t len;
    addr.sun_family = AF_LOCAL;
    /* use abstract namespace for socket path */
    addr.sun_path[0] = '\0';
    strcpy(&addr.sun_path[1], LOCAL_SOCKET_SERVER_NAME );
    len = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&addr.sun_path[1]);

    LOGD("In clientSocketThreadNative() : Before creating socket");
    sk = socket(PF_LOCAL, SOCK_STREAM, 0);
    if (sk < 0) {
        err = errno;
        LOGD("%s: Cannot open socket: %s (%d)\n",
            __FUNCTION__, strerror(err), err);
        errno = err;
        return;
    }

    LOGD("In clientSocketThreadNative() : Before connecting to Java LocalSocketServer");
    if (connect(sk, (struct sockaddr *) &addr, len) < 0) {
        err = errno;
        LOGD("%s: connect() failed: %s (%d)\n",
            __FUNCTION__, strerror(err), err);
        close(sk);
        errno = err;
        return;
    }

    LOGD("In clientSocketThreadNative() : Connecting to Java LocalSocketServer succeed");
    while(!stopThread){
        result = write(sk, buffer, 8);
        LOGD("In clientSocketThreadNative() : Total write = %d", result);
        count++;
        if(4 == count){
            sleep(1);
            count = 0;
        } 
    }


    LOGD("In clientSocketThreadNative() : End");
}


JNIEXPORT void JNICALL Java_test_socket_TestSocketActivity_setStopThreadNative
  (JNIEnv *env, jobject object){

    stopThread = 1;

}

你的例子在同一个进程中。 - Lewis Z

4

查看 Android 源代码中的 local_socket_client.c,看起来他们是这样做的:

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:
            namelen  = strlen(name);

            // Test with length +1 for the *initial* '\0'.
            if ((namelen + 1) > sizeof(p_addr->sun_path)) {
                goto error;
            }

            /*
             * Note: The path in this case is *not* supposed to be
             * '\0'-terminated. ("man 7 unix" for the gory details.)
             */

            p_addr->sun_path[0] = 0;
            memcpy(p_addr->sun_path + 1, name, namelen);

            ...
    p_addr->sun_family = AF_LOCAL;
    *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;

看起来memset()很重要,因为整个sun_path相关。(不过你的结构初始化貌似已经覆盖了这部分。)并且它不是“0”加上原始名称,而是一个实际的零字节!(它的值全部是二进制零,而不是ASCII '0')。
试着更仔细地跟随他们的做法,包括前导的'\0'字节和AF_LOCAL族。
如果您有更新的代码(无论它是否工作),请发布它!我对你的结果很感兴趣。你最后搞定了吗?
如果它不工作,请找出errno是什么,要么调用perror()将其打印到stderr,要么调用strerror()并记录输出。让我们知道你得到了什么错误。
编辑
最近我在我的一个项目中解决了这个问题。我发现关键在于在调用connect()和bind()时指定正确的长度。在我上面发布的代码中,它通过使用结构中sun_path的偏移量、名称的长度以及前导的'0'字节的长度来计算长度。如果您指定其他长度,Java代码可能无法连接到套接字。

好的,谢谢你的建议,Mike。经过一些测试,我认为我已经理解了它的思路。我会再试一次并尽快发布结果。 - Bohan Lu
@Bohan,看一下我的最近编辑。我已经让它工作了。我可以在Java代码中打开本地套接字,并在本机代码中连接到它们,反之亦然。关键是要正确获取“长度”字段。这与我Linux系统上的man 7 unix所说的不符。sizeof(sockaddr_un)不起作用。套接字的长度必须是sun_path的偏移量加上套接字名称中的字符数,再加上名称中前导零字节的1。(呃) - mpontillo
谢谢您的评论,我现在正在为我的项目苦苦挣扎另一个问题,但我会测试您建议的并尽快发布修改后的Java和本地代码... - Bohan Lu

1
确认以上内容正常工作。以下示例不仅可以一起使用,还可以与相应的Android LocalSocket和LocalServerSocket类一起使用:
客户端(Android是使用LocalServerSocket的服务器):
#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <sys/un.h>     /* struct sockaddr_un */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for close() */
#include <errno.h>
#include <stddef.h>

#define RCVBUFSIZE 2048   /* Size of receive buffer */

void DieWithError(char *errorMessage)  /* Error handling function */
{
    fprintf(stderr, "Error: %s - %s\n", errorMessage, strerror(errno));
    exit(errno);
}

int main(int argc, char *argv[])
{
    int sock;                        /* Socket descriptor */
    struct sockaddr_un echoServAddr; /* Echo server address */
    unsigned char *localSocketName = "MyTestSocket";
    static unsigned char echoString[] = {0x80, 0x00, 0x0e, 0x10, 0x00, 0x9c, 0x40, 0xc9, 0x20, 0x20, 0x20, 0x32, 0x00, 0x00};
    static unsigned int echoStringLen = sizeof(echoString); /* Length of string to echo */
    unsigned char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
    int bytesRcvd, totalBytesRcvd;   /* Bytes read in single recv() 
                                    and total bytes read */
    int size;
    int i;

    /* Create a reliable, stream socket using Local Sockets */
    if ((sock = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0)
        DieWithError("socket() failed");

    /* Construct the server address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));     /* Zero out structure */
    echoServAddr.sun_family      = AF_LOCAL;            /* Local socket address family */
    /**
     * Tricky and obscure! For a local socket to be in the "Android name space":
     * - The name of the socket must have a 0-byte value as the first character
     * - The linux man page is right in that 0 bytes are NOT treated as a null terminator.
     * - The man page is not clear in its meaning when it states that "the rest of the bytes in
     *        sunpath" are used. "Rest of bytes" is determined by the length passed in for
     *        sockaddr_len and Android sets this per the recommended file-system sockets of
     *        sizeof(sa_family_t) + strlen(sun_path) + 1. This is important when making calls
     *        to bind, connect, etc!
     * We have initialized the struct sockaddr_un to zero already, so all that is needed is to
     * begin the name copy at sun_path[1] and restrict its length to sizeof(echoServAddr.sun_path)-2
     **/
    strncpy(echoServAddr.sun_path + 1, localSocketName, sizeof(echoServAddr.sun_path) - 2); 
    size = sizeof(echoServAddr) - sizeof(echoServAddr.sun_path) + strlen(echoServAddr.sun_path+1) + 1;

    /* Establish the connection to the echo server */
    if (connect(sock, (struct sockaddr *) &echoServAddr, size) < 0)
        DieWithError("connect() failed");

    /* Send the string to the server */
    if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
        DieWithError("send() sent a different number of bytes than expected");

    /* Receive the same string back from the server */
    totalBytesRcvd = 0;
    printf("Sent:     ");
    for (i = 0; i < echoStringLen; i++)
        printf("%02X ", echoString[i]);
    printf("\n");    /* Print a final linefeed */

    printf("Received: ");                /* Setup to print the echoed string */
    if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
        DieWithError("recv() failed or connection closed prematurely");
    for (i = 0; i < bytesRcvd; i++)
        printf("%02X ", echoBuffer[i]);
    printf("\n");    /* Print a final linefeed */

    close(sock);
    exit(0);
}

服务器端(Android作为客户端使用LocalSocket):
#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <sys/un.h>     /* struct sockaddr_un */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for close() */
#include <errno.h>
#include <stddef.h>

#define RCVBUFSIZE 2048   /* Size of receive buffer */

void DieWithError(char *errorMessage)  /* Error handling function */
{
    fprintf(stderr, "Error: %s - %s\n", errorMessage, strerror(errno));
    exit(errno);
}

void HandleLocalClient(int clntSocket)
{
    char echoBuffer[RCVBUFSIZE];        /* Buffer for echo string */
    int recvMsgSize;                    /* Size of received message */

    /* Receive message from client */
    if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
        DieWithError("recv() failed");

    /* Send received string and receive again until end of transmission */
    while (recvMsgSize > 0)      /* zero indicates end of transmission */
    {
        /* Echo message back to client */
        if (send(clntSocket, echoBuffer, recvMsgSize, 0) != recvMsgSize)
            DieWithError("send() failed");

        /* See if there is more data to receive */
        if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
            DieWithError("recv() failed");
    }

    close(clntSocket);    /* Close client socket */
}

#define MAXPENDING 5    /* Maximum outstanding connection requests */

void DieWithError(char *errorMessage);  /* Error handling function */
void HandleLocalClient(int clntSocket);   /* TCP client handling function */

int main(int argc, char *argv[])
{
    int servSock;                    /* Socket descriptor for server */
    int clntSock;                    /* Socket descriptor for client */
    struct sockaddr_un echoClntAddr; /* Client address */
    unsigned int clntLen;            /* Length of client address data structure */
    struct sockaddr_un echoServAddr; /* Echo server address */
    unsigned char *localSocketName = "MyTestSocket";
    static unsigned int echoStringLen = 14;      /* Length of string to echo */
    unsigned char echoBuffer[RCVBUFSIZE];     /* Buffer for echo string */
    int bytesRcvd, totalBytesRcvd;   /* Bytes read in single recv() 
                                    and total bytes read */
    int size;
    int i;

    /* Create a reliable, stream socket using Local Sockets */
    if ((servSock = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0)
        DieWithError("socket() failed");

    /* Construct the server address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));     /* Zero out structure */
    echoServAddr.sun_family      = AF_LOCAL;            /* Local socket address family */
    /**
     * Tricky and obscure! For a local socket to be in the "Android name space":
     * - The name of the socket must have a 0-byte value as the first character
     * - The linux man page is right in that 0 bytes are NOT treated as a null terminator.
     * - The man page is not clear in its meaning when it states that "the rest of the bytes in
     *        sunpath" are used. "Rest of bytes" is determined by the length passed in for
     *        sockaddr_len and Android sets this per the recommended file-system sockets of
     *        sizeof(sa_family_t) + strlen(sun_path) + 1. This is important when making calls
     *        to bind, connect, etc!
     * We have initialized the struct sockaddr_un to zero already, so all that is needed is to
     * begin the name copy at sun_path[1] and restrict its length to sizeof(echoServAddr.sun_path)-2
     **/
    strncpy(echoServAddr.sun_path + 1, localSocketName, sizeof(echoServAddr.sun_path) - 2); 
    size = sizeof(echoServAddr) - sizeof(echoServAddr.sun_path) + strlen(echoServAddr.sun_path+1) + 1;

    /* Bind to the local address */
    if (bind(servSock, (struct sockaddr *) &echoServAddr, size) < 0)
        DieWithError("bind() failed");

    /* Mark the socket so it will listen for incoming connections */
    if (listen(servSock, MAXPENDING) < 0)
        DieWithError("listen() failed");

    for (;;) /* Run forever */
    {
        /* Set the size of the in-out parameter */
        clntLen = sizeof(echoClntAddr);

        /* Wait for a client to connect */
        if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0)
            DieWithError("accept() failed");

        /* clntSock is connected to a client! */

        printf("Handling client\n");

        HandleLocalClient(clntSock);
    }
    /* NOT REACHED */
    return 0;
}

已测试通过 Android 4.0.3 ICS 03/15


0

感谢这里提供的答案。综合各位用户的回复,需要注意以下事项:

在本地端:

  1. 套接字名称的第一个字符必须为0字节值
  2. Linux手册中指出0字节不会被视为空终止符。
  3. 当man手册声明“sunpath中剩余的字节”时,其含义并不清晰。“剩余的字节”由sockaddr_len传递的长度确定,并且Android根据建议的文件系统套接字设置为 sizeof(sa_family_t) + strlen(sun_path) + 1。在调用bind、connect等操作时这点非常重要!

在Java端:

Android定义了其内部使用的约定,但与上述内容一致。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接