Example #1
0
 public void run() {
   ls = new LocalSocket();
   lsa = new LocalSocketAddress("/data/local/tmp/ShellPhoneSocket");
   try {
     ls.bind(lsa);
     ls.connect(lsa);
   } catch (IOException e) {
     e.printStackTrace();
   }
   while (!Thread.currentThread().isInterrupted()) {
     try {
       BufferedReader in = new BufferedReader(new InputStreamReader(ls.getInputStream()));
       while ((line = in.readLine()) != null) {
         handler.post(
             new Runnable() {
               @Override
               public void run() {
                 call(line);
                 // update
               }
             });
       }
       break;
     } catch (Exception e) {
       handler.post(
           new Runnable() {
             @Override
             public void run() {
               // interrupted connection
             }
           });
       e.printStackTrace();
     }
   }
 }
  public void testAccessors() throws IOException {
    LocalSocket socket = new LocalSocket();
    LocalSocketAddress addr = new LocalSocketAddress("secondary");

    assertFalse(socket.isBound());
    socket.bind(addr);
    assertTrue(socket.isBound());
    assertEquals(addr, socket.getLocalSocketAddress());

    String str = socket.toString();
    assertTrue(str.contains("impl:android.net.LocalSocketImpl"));

    socket.setReceiveBufferSize(1999);
    assertEquals(1999 << 1, socket.getReceiveBufferSize());

    socket.setSendBufferSize(3998);
    assertEquals(3998 << 1, socket.getSendBufferSize());

    // Timeout is not support at present, so set is ignored
    socket.setSoTimeout(1996);
    assertEquals(0, socket.getSoTimeout());

    try {
      socket.getRemoteSocketAddress();
      fail("testLocalSocketSecondary shouldn't come to here");
    } catch (UnsupportedOperationException e) {
      // expected
    }

    try {
      socket.isClosed();
      fail("testLocalSocketSecondary shouldn't come to here");
    } catch (UnsupportedOperationException e) {
      // expected
    }

    try {
      socket.isInputShutdown();
      fail("testLocalSocketSecondary shouldn't come to here");
    } catch (UnsupportedOperationException e) {
      // expected
    }

    try {
      socket.isOutputShutdown();
      fail("testLocalSocketSecondary shouldn't come to here");
    } catch (UnsupportedOperationException e) {
      // expected
    }

    try {
      socket.connect(addr, 2005);
      fail("testLocalSocketSecondary shouldn't come to here");
    } catch (UnsupportedOperationException e) {
      // expected
    }
  }