Interface

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 for receiving and reading packets.

A packet usually has a limited size and may be empty. Different packet socket types have different maximum packet sizes. For example, the UDP socket can process a maximum of 64 KB packet at a time. Packet transmission is not reliable, and the transmission sequence is not guaranteed. The size of a 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 Type:

prop localAddress

prop localAddress: SocketAddress

Description: Reads the local address bound or to be bound with Socket.

Type: SocketAddress

Throws:

  • SocketException: This exception is thrown when Socket is closed or no local address is available (the local address is not configured and the socket is not connected).

prop receiveTimeout

mut prop receiveTimeout: ?Duration

Description: Sets and reads the timeout interval of receiveFrom. If there is no timeout interval, None is used.

If the interval is too small, the minimum clock cycle is used. If the interval is too large, the maximum timeout interval (263–1 ns) is used. The default value is None.

Type: ?Duration

Throws:

prop remoteAddress

prop remoteAddress: ?SocketAddress

Description: Reads the remote address connected to Socket. If Socket is not connected, None is returned.

Type: ?SocketAddress

Throws:

prop sendTimeout

mut prop sendTimeout: ?Duration

Description: Sets and reads the timeout interval of sendTo. By default, it is set to None.

If the interval is too small, the minimum clock cycle is used. If the interval is too large, the maximum timeout interval (263–1 ns) is used. The default value is None.

Type: ?Duration

Throws:

func receiveFrom(Array<Byte>)

func receiveFrom(buffer: Array<Byte>): (SocketAddress, Int64)

Description: Waits in a blocked state for receiving packets in buffer.

Parameters:

  • buffer: Array<Byte>: Buffer space for storing the content of packets. buffer should have a proper size. Otherwise, the packet to be received may be truncated, and the size of the packet is greater than the value of buffer.

Returns:

  • (SocketAddress, Int64): packet sending address and size of the received packet (which may be 0 or greater than the value of buffer)

Throws:

func sendTo(SocketAddress, Array<Byte>)

func sendTo(address: SocketAddress, payload: Array<Byte>): Unit

Description: Sends packets to a specified remote address. When the peer end does not have sufficient buffer, this operation may be blocked and packets may be discarded.

Parameters:

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 Type:

prop localAddress

prop localAddress: SocketAddress

Description: Reads the local address bound or to be bound with Socket.

Type: SocketAddress

Throws:

func accept()

func accept(): StreamingSocket

Description: Receives a connection request from a client socket and waits in a blocked state for the connection request.

Returns:

func accept(?Duration)

func accept(timeout!: ?Duration): StreamingSocket

Description: Receives a connection request from a client socket and waits for the connection request in blocking mode.

Parameters:

  • timeout!: ?Duration: timeout interval for waiting for a connection

Returns:

Throws:

func bind()

func bind(): Unit

Description: Binds a socket.

If the reuse attribute is not adopted, the close socket is needed 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 is readable and writable.

StreamingSocket can be bound (bind) and connected (connect). You can set the remote and local addresses for the binding and connection through attribute setting. StreamingSocket can transmit byte streams in sequenced packets. A buffer space is used to store read and written byte streams. By default, when no data arrives, the read interface (read()) waits in a blocked state until arrival of subsequent data or a timeout. The write operation (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 a blocked state for spare buffer space or a timeout. Characters are always read and 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. When an abnormal packet is received, the packet length of –1 is returned.

Parent Type:

prop localAddress

prop localAddress: SocketAddress

Description: Reads the local address bound or to be bound with Socket.

Type: SocketAddress

Throws:

  • SocketException: This exception is thrown when Socket is closed or no local address is available (the local address is not configured and the socket is not connected).

prop readTimeout

mut prop readTimeout: ?Duration

Description: Sets and reads the read timeout interval.

If the interval is too small, the minimum clock cycle is used. If the interval is too large, the maximum timeout interval (263–1 ns) is used. The default value is None.

Type: ?Duration

Throws:

prop remoteAddress

prop remoteAddress: SocketAddress

Description: Reads the remote address bound or to be bound with Socket.

Type: SocketAddress

Throws:

prop writeTimeout

mut prop writeTimeout: ?Duration

Description: Sets and reads the write timeout interval.

If the interval is too small, the minimum clock cycle is used. If the interval is too large, the maximum timeout interval (263–1 ns) is used. The default value is None.

Type: ?Duration

Throws: