Class SSLSocket

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class SSLSocket
    extends Socket
    This class extends Sockets and provides secure socket using protocols such as the "Secure Sockets Layer" (SSL) or IETF "Transport Layer Security" (TLS) protocols.
    The initial handshake on this connection is initiated by calling startHandshake which explicitly begins handshakes. If handshaking fails for any reason, the SSLSocket is closed, and no further communications can be done.
    • Constructor Detail

      • SSLSocket

        protected SSLSocket​(SSLContextSpi sslContext,
                            java.lang.String host,
                            int port,
                            int timeout)
                     throws UnknownHostException,
                            IOException
        Constructs an SSL connection to a named host at a specified port, with the specified connection timeout, binding the client side of the connection a given address and port. This acts as the SSL client.
        Parameters:
        host - the server's host
        port - its port
        timeout - the timeout for this operation
        Throws:
        UnknownHostException - if the host is not known
        IOException - if an I/O error occurs when creating the socket
    • Method Detail

      • prepareContext

        protected SSLClient prepareContext()
                                    throws CryptoException
        Creates a new SSLClient to be used by this instance of SSLSocket during the handshake. The default implementation does not perform any kind of validation. Subclasses may override this method to use their own implementation of SSLClient.
        Returns:
        a SSLClient initialized with the objects required to perform the validation for this socket.
        Throws:
        CryptoException
      • startHandshake

        public void startHandshake()
                            throws IOException
        Starts an SSL handshake on this connection.
        Throws:
        IOException - on a network level error
      • readBytes

        public int readBytes​(byte[] buf,
                             int start,
                             int count)
                      throws IOException
        Description copied from class: Socket
        Reads bytes from the socket into a byte array. Note that in the device it may return with less bytes than requested. Note also that, if -1 is returned and the lastError is 4626, please insist 3 or 4 more times, because it may work.
        Overrides:
        readBytes in class Socket
        Parameters:
        buf - the byte array to read data into
        start - the start position in the byte array
        count - the number of bytes to read
        Returns:
        The number of bytes actually read; or <= 0 if no data is available; or -1 if the server closed the connection or an error prevented the read operation from occurring.
        Throws:
        IOException
      • writeBytes

        public int writeBytes​(byte[] buf,
                              int start,
                              int count)
                       throws IOException
        Description copied from class: Socket
        Writes to the socket. Returns the number of bytes written or -1 if an error prevented the write operation from occurring. If data can't be written to the socket for approximately 2 seconds, the write operation will time out.
        Overrides:
        writeBytes in class Socket
        Parameters:
        buf - the byte array to write data from
        start - the start position in the byte array
        count - the number of bytes to write
        Returns:
        the number of bytes actually written
        Throws:
        IOException
      • close

        public void close()
                   throws IOException
        Description copied from class: Socket
        Closes the socket.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class Socket
        Throws:
        IOException - If the socket is closed more than once