Here are the examples of the java api class android.net.LocalSocket taken from open source projects.
1. MediaStream#createSockets()
Project: spydroid-ipcamera
File: MediaStream.java
File: MediaStream.java
protected void createSockets() throws IOException { final String LOCAL_ADDR = "net.majorkernelpanic.streaming-"; for (int i = 0; i < 10; i++) { try { mSocketId = new Random().nextInt(); mLss = new LocalServerSocket(LOCAL_ADDR + mSocketId); break; } catch (IOException e1) { } } mReceiver = new LocalSocket(); mReceiver.connect(new LocalSocketAddress(LOCAL_ADDR + mSocketId)); mReceiver.setReceiveBufferSize(500000); mReceiver.setSoTimeout(3000); mSender = mLss.accept(); mSender.setSendBufferSize(500000); }
2. IpcConnection#connect()
Project: OSMonitor
File: IpcConnection.java
File: IpcConnection.java
public boolean connect(int timeOut) throws IOException { // use abstract or file system Unix domain socket if (!socketName.startsWith("/")) clientAddress = new LocalSocketAddress(socketName, LocalSocketAddress.Namespace.ABSTRACT); else clientAddress = new LocalSocketAddress(socketName, LocalSocketAddress.Namespace.FILESYSTEM); clientSocket = new LocalSocket(); clientSocket.connect(clientAddress); clientSocket.setSendBufferSize(sendBufferSize); clientSocket.setReceiveBufferSize(recvBufferSize); // Notice: the value is milliseconds clientSocket.setSoTimeout(timeOut * 1000); return true; }
3. PowerTestHostLink#run()
Project: CtsVerifier
File: PowerTestHostLink.java
File: PowerTestHostLink.java
/** * Run the suite of tests via the host, responding to host requests. * * @return number of failed test cases * @throws Exception */ public PowerTestResult run() throws Exception { // define buffer to receive data from host final int BUFFER_SIZE = 4096; byte[] buffer = new byte[BUFFER_SIZE]; LocalServerSocket serverSocket = createSocket(); if (null == serverSocket) { mStopThread = true; } InputStream streamIn; OutputStream streamOut; LocalSocket receiverSocket; while (!mStopThread) { try { Log.d(TAG, "localSocketServer accept..."); receiverSocket = serverSocket.accept(); Log.d(TAG, "Got new connection"); } catch (IOException e) { Log.d(TAG, "localSocketServer accept() failed !!!", e); continue; } try { streamIn = receiverSocket.getInputStream(); } catch (IOException e) { Log.d(TAG, "getInputStream() failed !!!", e); continue; } try { streamOut = receiverSocket.getOutputStream(); } catch (IOException e) { Log.e(TAG, "getOutputStream() failed", e); continue; } Log.d(TAG, "The client connected to LocalServerSocket"); try { int total = 0; // command and response handshake, so read all data while (streamIn.available() > 0 || total == 0) { if (total < BUFFER_SIZE) { int bytesRead = streamIn.read(buffer, total, (BUFFER_SIZE - total)); if (bytesRead > 0) { total += bytesRead; } } else { Log.e(TAG, "Message too long: truncating"); } } String clientRequest = new String(buffer); clientRequest = clientRequest.substring(0, total); if (clientRequest.length() > 0) { Log.d(TAG, "Client requested: " + clientRequest); try { String response = processClientRequest(clientRequest); if (response != null) { Log.d(TAG, "Sending response " + response); streamOut.write(response.getBytes(), 0, response.length()); } // null response means response is deferred awaiting user response } catch (Exception e) { Log.e(TAG, "Error executing " + clientRequest, e); streamOut.write(RESPONSE_ERR.getBytes(), 0, RESPONSE_ERR.length()); } } receiverSocket.close(); } catch (IOException e) { Log.e(TAG, "There is an exception when reading from or writing to socket", e); break; } } Log.d(TAG, "The LocalSocketServer thread is going to stop !!!"); if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { Log.d(TAG, "Exception on close of server socket", e); } } mHostToDeviceExecutor.logText("Device disconnected."); Log.d(TAG, "Returning " + mTestResult.passedCount + "passed " + mTestResult.skippedCount + "skipped " + mTestResult.failedCount + "failed."); return mTestResult; }
4. SamsungMulticlientRilExecutor#detect()
Project: Android-IMSI-Catcher-Detector
File: SamsungMulticlientRilExecutor.java
File: SamsungMulticlientRilExecutor.java
@Override public DetectResult detect() { String gsmVerRilImpl = ""; try { Class clazz; clazz = Class.forName("android.os.SystemProperties"); Method method = clazz.getDeclaredMethod("get", String.class, String.class); gsmVerRilImpl = (String) method.invoke(null, "gsm.version.ril-impl", ""); } catch (Exception ignore) { log.debug("ignore this exception?", ignore); } // WARNING may have bad consequences... if (!gsmVerRilImpl.matches("Samsung\\s+RIL\\(IPC\\).*")) { return DetectResult.Unavailable("gsm.version.ril-impl = " + gsmVerRilImpl); } LocalSocket s = new LocalSocket(); try { s.connect(new LocalSocketAddress(MULTICLIENT_SOCKET)); } catch (IOException e) { log.warn(e.getMessage()); return DetectResult.Unavailable("Multiclient socket is not available\n" + "gsm.version.ril-impl = " + gsmVerRilImpl); } finally { try { s.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } return DetectResult.AVAILABLE; }