title: Android LocalServerSocket
body: <p>In android, there are two classes LocalServerSocket and LocalSocket. I think they are something like AF_LOCAL in unix socket (I am not sure it is correct or not).</p>
  
  <p>My question is that :
  Is it possible to create LocalServerSocket in Java and use a normal unix socket client to connect to it in native or other process ?</p>
  
  <p>If it is possible, what the  "sockaddr_un.sun_path"  I should set in native ?</p>
  
  <p>I have written a sample project to test it, and I try to set the .sun_path as same as string name used in LocalServerSocket, but it failed, the native could not connect to the Java LocalServerSocket.</p>
  
  <p>My Java code :</p>
  
  <pre><code>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 &gt;= 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);
      }
  
  }
  </code></pre>
  
  <p>My Native code :</p>
  
  <pre><code>#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&lt;8; i++){
          buffer[i] = (i+1);
      }
  
      /*
      struct sockaddr_un addr;
  
      bzero((char *)&amp;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 &lt; 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 *) &amp;addr, sizeof(addr)) &lt; 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");
  }
  </code></pre>
  
  <p>Any suggestion would be greatly appreciated !!!</p>