Package totalcross.net.ssl
Class SSLSocket
- java.lang.Object
-
- totalcross.io.Stream
-
- totalcross.net.Socket
-
- totalcross.net.ssl.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.
-
-
Field Summary
-
Fields inherited from class totalcross.net.Socket
DEFAULT_OPEN_TIMEOUT, DEFAULT_READ_TIMEOUT, DEFAULT_WRITE_TIMEOUT, readTimeout, writeTimeout
-
Fields inherited from class totalcross.io.Stream
skipBuffer
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedSSLSocket(SSLContextSpi sslContext, java.lang.String host, int port, int timeout)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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the socket.protected SSLClientprepareContext()Creates a new SSLClient to be used by this instance of SSLSocket during the handshake.intreadBytes(byte[] buf, int start, int count)Reads bytes from the socket into a byte array.voidstartHandshake()Starts an SSL handshake on this connection.intwriteBytes(byte[] buf, int start, int count)Writes to the socket.-
Methods inherited from class totalcross.net.Socket
finalize, getHost, getNativeSocket, getPort, readBytes, readLine
-
Methods inherited from class totalcross.io.Stream
asInputStream, asOutputStream, asStream, asStream, skipBytes, wrapInputStream, wrapInputStreamToStream, write, writeBytes, writeBytes, writeBytes
-
-
-
-
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 hostport- its porttimeout- the timeout for this operation- Throws:
UnknownHostException- if the host is not knownIOException- 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 IOExceptionStarts an SSL handshake on this connection.- Throws:
IOException- on a network level error
-
readBytes
public int readBytes(byte[] buf, int start, int count) throws IOExceptionDescription copied from class:SocketReads 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:
readBytesin classSocket- Parameters:
buf- the byte array to read data intostart- the start position in the byte arraycount- 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 IOExceptionDescription copied from class:SocketWrites 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:
writeBytesin classSocket- Parameters:
buf- the byte array to write data fromstart- the start position in the byte arraycount- the number of bytes to write- Returns:
- the number of bytes actually written
- Throws:
IOException
-
close
public void close() throws IOExceptionDescription copied from class:SocketCloses the socket.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classSocket- Throws:
IOException- If the socket is closed more than once
-
-