Interfaces
interface DatagramSocket
public interface DatagramSocket <: Resource & ToString {
prop localAddress: SocketAddress
prop remoteAddress: ?SocketAddress
mut prop receiveTimeout: ?Duration
mut prop sendTimeout: ?Duration
func receiveFrom(buffer: Array<Byte>): (SocketAddress, Int64)
func sendTo(address: SocketAddress, payload: Array<Byte>): Unit
}
Description: DatagramSocket is a socket that receives and reads data packets.
A data packet usually has a limited size and may be empty. The maximum data packet size varies with the data packet socket type. For example, a UDP socket can process a data pocket of up to 64 KB at a time.
Data packet transmission is not reliable, and the transmission sequence is not guaranteed. The size of a data packet is determined by the transmit end. For example, if a 10-byte packet and a 15-byte packet are sent from one end, the peer end receives the packets of the same sizes: 10 bytes and 15 bytes.
Parent types:
prop localAddress
prop localAddress: SocketAddress
Description: Reads the local address of Socket that is to be or has been bound.
Type: SocketAddress
Throws:
- SocketException: If
Sockethas been closed or no local address is available (no local address is configured and the socket is not connected), this exception is thrown.
prop receiveTimeout
mut prop receiveTimeout: ?Duration
Description: Sets and reads the timeout interval of receiveFrom. If no timeout interval is set, None is returned.
If the value is too small, the minimum clock cycle value is used. If the value is too large, the maximum timeout interval (263 – 1 nanoseconds) is used. The default value is None.
Type: ?Duration
Throws:
- IllegalArgumentException: If the timeout interval is less than 0, this exception is thrown.
prop remoteAddress
prop remoteAddress: ?SocketAddress
Description: Reads the remote address connected to Socket. If Socket is not connected, None is returned.
Type: ?SocketAddress
Throws:
- SocketException: If
Sockethas been closed, this exception is thrown.
prop sendTimeout
mut prop sendTimeout: ?Duration
Description: Sets and reads the timeout interval of sendTo. The default value is None.
If the value is too small, the minimum clock cycle value is used. If the value is too large, the maximum timeout interval (263 – 1 nanoseconds) is used. The default value is None.
Type: ?Duration
Throws:
- IllegalArgumentException: If the timeout interval is less than 0, this exception is thrown.
func receiveFrom(Array<Byte>)
func receiveFrom(buffer: Array<Byte>): (SocketAddress, Int64)
Description: Waits in blocking mode to receive packets to buffer.
Parameters:
- buffer: Array<Byte>: buffer space for storing packets. The
buffersize should be proper. Otherwise, packets may be truncated during receiving, and the size of returned packets is greater than that ofbuffer.
Returns:
- (SocketAddress, Int64): packet sending address and received packet size (which may be 0 or greater than the size of
buffer).
Throws:
- SocketException: If the local buffer is too small to read packets, this exception is thrown.
- SocketTimeoutException: If the read operation times out, this exception is thrown.
func sendTo(SocketAddress, Array<Byte>)
func sendTo(address: SocketAddress, payload: Array<Byte>): Unit
Description: Sends packets to a specified remote address. If the peer end does not have sufficient buffer, this operation may be blocked and packets may be discarded.
Parameters:
- address: SocketAddress: remote address to which packets are sent
- payload: Array<Byte>: content of packets to be sent
interface ServerSocket
public interface ServerSocket <: Resource & ToString {
prop localAddress: SocketAddress
func accept(): StreamingSocket
func accept(timeout!: ?Duration): StreamingSocket
func bind(): Unit
}
Description: Provides APIs required by Socket of the server.
Parent types:
prop localAddress
prop localAddress: SocketAddress
Description: Reads the local address of Socket that is to be or has been bound.
Type: SocketAddress
Throws:
- SocketException: If
Sockethas been closed, this exception is thrown.
func accept()
func accept(): StreamingSocket
Description: Receives a connection request from a client socket and waits in blocking mode for the connection request.
Returns:
- StreamingSocket: client socket that is successfully connected
func accept(?Duration)
func accept(timeout!: ?Duration): StreamingSocket
Description: Receives a connection request from a client socket and waits in blocking mode for the connection request.
Parameters:
- timeout!: ?Duration: timeout interval for waiting for a connection
Returns:
- StreamingSocket: client socket that is successfully connected
Throws:
- SocketTimeoutException: If a timeout occurs in waiting for the connection request, this exception is thrown.
- IllegalArgumentException: If the timeout interval is less than 0, this exception is thrown.
func bind()
func bind(): Unit
Description: Binds the socket.
If the reuse attribute is not set, close is used to close the socket when the local port, address, or file path is occupied, or the last connection to the bound socket fails. The accept() operation can be performed after multiple retries.
Throws:
- SocketException: If binding fails due to a system error, this exception is thrown.
interface StreamingSocket
public interface StreamingSocket <: IOStream & Resource & ToString {
prop localAddress: SocketAddress
prop remoteAddress: SocketAddress
mut prop readTimeout: ?Duration
mut prop writeTimeout: ?Duration
}
Description: Specifies Socket running in duplex stream mode, which can be read or written.
StreamingSocket can be bound (bind) and connected (connect). You can set the remote and local addresses of the binding and connection through attributes.
StreamingSocket can transmit byte streams in sequenced packets. A buffer space is used to store read or written byte stream. By default, when no data arrives, the read interface (read()) waits in blocking mode until the next arrival of subsequent data or a timeout. The write interface (write()) writes data in the buffer and sends the data later. If the buffer space is insufficient or the write speed is faster than the forwarding speed, the write operation waits in blocking mode for spare buffer space or a timeout.
Characters are always read or written in order, but the packet policy and size during transmission may not be the same as those during packet delivery. For example, if one end sends a 10-byte packet and then a 15-byte packet, the peer end may receive the 10-byte packet and the 15-byte packet respectively, or may receive a 25-byte packet at a time.
If an abnormal packet is received, the packet length of –1 is returned.
Parent types:
prop localAddress
prop localAddress: SocketAddress
Description: Reads the local address of Socket that is to be or has been bound.
Type: SocketAddress
Throws:
- SocketException: If
Sockethas been closed or no local address is available (no local address is configured and the socket is not connected), this exception is thrown.
prop readTimeout
mut prop readTimeout: ?Duration
Description: Sets and reads the read timeout interval.
If the value is too small, the minimum clock cycle value is used. If the value is too large, the maximum timeout interval (263 – 1 nanoseconds) is used. The default value is None.
Type: ?Duration
Throws:
- IllegalArgumentException: If the timeout interval is less than 0, this exception is thrown.
prop remoteAddress
prop remoteAddress: SocketAddress
Description: Reads the remote address bound or to be bound with Socket.
Type: SocketAddress
Throws:
- SocketException: If
Sockethas been closed, this exception is thrown.
prop writeTimeout
mut prop writeTimeout: ?Duration
Description: Sets and reads the write timeout interval.
If the value is too small, the minimum clock cycle value is used. If the value is too large, the maximum timeout interval (263 – 1 nanoseconds) is used. The default value is None.
Type: ?Duration
Throws:
- IllegalArgumentException: If the timeout interval is less than 0, this exception is thrown.