Code example for LocalSocket

Methods: close, connect, getFileDescriptor, getInputStream, getOutputStream, getPeerCredentials, isConnected, setFileDescriptorsForSend, shutdownInput, shutdownOutput

0
        ) 
    }) 
    public void testLocalConnections() throws IOException{ 
        // create client and server socket 
        LocalServerSocket localServerSocket = new LocalServerSocket(mSockAddr);
        LocalSocket clientSocket = new LocalSocket();
 
        // establish connection between client and server 
        LocalSocketAddress locSockAddr = new LocalSocketAddress(mSockAddr);
        assertFalse(clientSocket.isConnected());
        clientSocket.connect(locSockAddr);
        assertTrue(clientSocket.isConnected());
        LocalSocket serverSocket = localServerSocket.accept();
 
        Credentials credent = clientSocket.getPeerCredentials();
        assertTrue(0 != credent.getPid());
 
        // send data from client to server 
        OutputStream clientOutStream = clientSocket.getOutputStream();
        clientOutStream.write(12);
        InputStream serverInStream = serverSocket.getInputStream();
        assertEquals(12, serverInStream.read());
 
        //send data from server to client 
        OutputStream serverOutStream = serverSocket.getOutputStream();
        serverOutStream.write(3);
        InputStream clientInStream = clientSocket.getInputStream();
        assertEquals(3, clientInStream.read());
 
        // Test sending and receiving file descriptors 
        clientSocket.setFileDescriptorsForSend(new FileDescriptor[]{FileDescriptor.in});
        clientOutStream.write(32);
        assertEquals(32, serverInStream.read());
 
        FileDescriptor[] out = serverSocket.getAncillaryFileDescriptors();
        assertEquals(1, out.length);
        FileDescriptor fd = clientSocket.getFileDescriptor();
        assertTrue(fd.valid());
 
        //shutdown input stream of client 
        clientSocket.shutdownInput();
        assertEquals(-1, clientInStream.read());
 
        //shutdown output stream of client 
        clientSocket.shutdownOutput();
        try { 
            clientOutStream.write(10);
            fail("testLocalSocket shouldn't come to here");
        } catch (IOException e) {
            // expected 
        } 
 
        //shutdown input stream of server 
        serverSocket.shutdownInput();
        assertEquals(-1, serverInStream.read());
 
        //shutdown output stream of server 
        serverSocket.shutdownOutput();
        try { 
            serverOutStream.write(10);
            fail("testLocalSocket shouldn't come to here");
        } catch (IOException e) {
            // expected 
        } 
 
        //close client socket 
        clientSocket.close();
        try { 
            clientInStream.read();
            fail("testLocalSocket shouldn't come to here");
        } catch (IOException e) {
            // expected