Code example for Socket

Methods: getLocalAddress, isClosed, isConnected

0
            throw new SSLPeerUnverifiedException(
                    "Certificate hostname not useable for server: " + hostname);
        } 
    } 
 
    /** 
     * Set the socket timeout. 
     * @param timeoutMilliseconds the read timeout value if greater than {@code 0}, or 
     *            {@code 0} for an infinite timeout. 
     */ 
    @Override 
    public void setSoTimeout(int timeoutMilliseconds) throws SocketException {
        mSocket.setSoTimeout(timeoutMilliseconds);
    } 
 
    @Override 
    public boolean isOpen() { 
        return (mIn != null && mOut != null &&
                mSocket != null && mSocket.isConnected() && !mSocket.isClosed());
    } 
 
    /** 
     * Close the connection.  MUST NOT return any exceptions - must be "best effort" and safe. 
     */ 
    @Override 
    public void close() { 
        try { 
            mIn.close();
        } catch (Exception e) {
            // May fail if the connection is already closed. 
        } 
        try { 
            mOut.close();
        } catch (Exception e) {
            // May fail if the connection is already closed. 
        } 
        try { 
            mSocket.close();
        } catch (Exception e) {
            // May fail if the connection is already closed. 
        } 
        mIn = null;
        mOut = null;
        mSocket = null;
    } 
 
    @Override 
    public InputStream getInputStream() {
        return mIn;
    } 
 
    @Override 
    public OutputStream getOutputStream() {
        return mOut;
    } 
 
    /** 
     * Writes a single line to the server using \r\n termination. 
     */ 
    @Override 
    public void writeLine(String s, String sensitiveReplacement) throws IOException {
        if (Email.DEBUG) { 
            if (sensitiveReplacement != null && !Logging.DEBUG_SENSITIVE) {
                Log.d(Logging.LOG_TAG, ">>> " + sensitiveReplacement);
            } else { 
                Log.d(Logging.LOG_TAG, ">>> " + s);
            } 
        } 
 
        OutputStream out = getOutputStream();
        out.write(s.getBytes());
        out.write('\r');
        out.write('\n');
        out.flush();
    } 
 
    /** 
     * Reads a single line from the server, using either \r\n or \n as the delimiter.  The 
     * delimiter char(s) are not included in the result. 
     */ 
    @Override 
    public String readLine() throws IOException {
        StringBuffer sb = new StringBuffer();
        InputStream in = getInputStream();
        int d;
        while ((d = in.read()) != -1) {
            if (((char)d) == '\r') {
                continue; 
            } else if (((char)d) == '\n') {
                break; 
            } else { 
                sb.append((char)d);
            } 
        } 
        if (d == -1 && Email.DEBUG) {
            Log.d(Logging.LOG_TAG, "End of stream reached while trying to read line.");