Class
class IPMask
public class IPMask <: ToString {
public init(buf: Array<UInt8>)
public init(ones: Int64, bits: Int64)
public init(a: UInt8, b: UInt8, c: UInt8, d: UInt8)
}
Description: Specifies an IP mask used to operate the IP address and route address.
Parent Type:
init(Array<UInt8>)
public init(buf: Array<UInt8>)
Description: Initializes a mask value.
Parameters:
Throws:
- IllegalArgumentException: If the value of
buf
exceeds the threshold, which does not meet IPv4 or IPv6 requirements, this exception is thrown.
init(Int64, Int64)
public init(ones: Int64, bits: Int64)
Description: Initializes a mask value.
Parameters:
Throws:
- IllegalArgumentException: If the value of
bits
exceeds the threshold, which does not meet IPv4 or IPv6 requirements, this exception is thrown.
init(UInt8, UInt8, UInt8, UInt8)
public init(a: UInt8, b: UInt8, c: UInt8, d: UInt8)
Description: Initializes a mask value. The parameter values are the four bytes a.b.c.d
of IPv4, respectively.
Parameters:
- a: UInt8: most significant byte
- b: UInt8: second most significant byte
- c: UInt8: second least significant byte
- d: UInt8: least significant byte
func size()
public func size(): (Int64, Int64)
Description: Obtains the number of 1s at the most significant bits and obtains the total number of bits
.
Returns:
- (Int64, Int64): number of 1s at the most significant bits and total number of
bits
. If any of the most significant bits of the mask is not 1,(0, 0)
is returned.
func toString()
public func toString(): String
Description: Converts an IP mask to a string.
Returns:
- String: string after conversion
class RawSocket
public class RawSocket {
public init(domain: SocketDomain, `type`: SocketType, protocol: ProtocolType)
}
Description: RawSocket provides basic functions of a socket.
This class can be used to access a socket combining specific communication domain, type, and protocol. The socket packet provides support for common network protocols including TCP and UDP. Therefore, this class is applicable to network programming using other protocols.
Note:
- Currently, verified functions of RawSocket include TCP, UDP, UDS, and ICMP sockets. For other classes, unexpected usage problems may occur.
- In addition, due to the API openness, using
connect
followed bylisten
is allowed. In some scenarios, unexpected problems may occur. It is recommended that developers comply with normal calling logic to prevent problems.
prop localAddr
public prop localAddr: RawAddress
Description: Obtains the local address of the current RawSocket instance.
Type: RawAddress
Throws:
- SocketException: If the current RawSocket instance has been closed or the local address cannot be obtained, this exception is thrown.
prop readTimeout
public mut prop readTimeout: ?Duration
Description: Obtains or sets the read timeout of the current RawSocket instance.
Type: ?Duration
Throws:
- SocketException: If the current RawSocket instance has been closed, this exception is thrown.
- IllegalArgumentException: If the set read timeout is negative, this exception is thrown.
prop remoteAddr
public prop remoteAddr: RawAddress
Description: Obtains the peer address of the current RawSocket instance.
Type: RawAddress
Throws:
- SocketException: If the current RawSocket instance has been closed or the peer address cannot be obtained, this exception is thrown.
prop writeTimeout
public mut prop writeTimeout: ?Duration
Description: Obtains or sets the write timeout of the current RawSocket instance.
Type: ?Duration
Throws:
- SocketException: If the current RawSocket instance has been closed, this exception is thrown.
- IllegalArgumentException: If the set write timeout is negative, this exception is thrown.
init(SocketDomain, SocketType, ProtocolType)
public init(domain: SocketDomain, `type`: SocketType, protocol: ProtocolType)
Description: Creates a socket combining specific communication domain, type, and protocol.
Parameters:
- domain: SocketDomain: communication domain
- `type`: SocketType: socket type
- protocol: ProtocolType: protocol type
Throws:
- SocketException: If the socket combining the specific communication domain, type, and protocol cannot be created, this exception is thrown.
func accept(?Duration)
public func accept(timeout!: ?Duration = None): RawSocket
Description: Receives the first connection request in the suspended connection queue when the current RawSocket instance is listening, and returns a RawSocket for communication.
Parameters:
- timeout!: ?Duration: maximum duration for waiting for the connection request. The default value
None
indicates always waiting.
Returns:
Throws:
- SocketException: If the current RawSocket instance has been closed or receiving fails, this exception is thrown.
- SocketTimeoutException: If waiting times out, this exception is thrown.
func bind(RawAddress)
public func bind(addr: RawAddress): Unit
Description: Binds the current RawSocket instance to a specified socket address.
Parameters:
- addr: RawAddress: socket address
Throws:
- SocketException: If the current RawSocket instance has been closed or binding fails, this exception is thrown.
func close()
public func close(): Unit
Description: Closes the current RawSocket instance.
func connect(RawAddress, ?Duration)
public func connect(addr: RawAddress, timeout!: ?Duration = None): Unit
Description: Sends a connection request to a destination address.
Parameters:
- addr: RawAddress: destination address to which the connection request is sent
- timeout!: ?Duration: maximum duration for receiving the connection request. The default value
None
indicates always waiting.
Throws:
- SocketException: If the current RawSocket instance has been closed or receiving fails, this exception is thrown.
- SocketTimeoutException: If waiting times out, this exception is thrown.
func getSocketOption(Int32, Int32, CPointer<Byte>, CPointer<Int32>)
public unsafe func getSocketOption(level: Int32, option: Int32, value: CPointer<Byte>, len: CPointer<Int32>): Unit
Description: Obtains the value of a socket option.
Parameters:
- level: Int32: socket option level
- option: Int32: socket option name
- value: CPointer<Byte>: socket option value
- len: CPointer<Int32>: socket option value length
Throws:
- SocketException: If the current RawSocket instance has been closed or the socket option fails to be obtained, this exception is thrown.
func listen(Int32)
public func listen(backlog: Int32): Unit
Description: Listens to the address bound to the current RawSocket instance.
Parameters:
- backlog: Int32: maximum length of the waiting queue
Throws:
- SocketException: If the current RawSocket instance has been closed or listening fails, this exception is thrown.
func receive(Array<Byte>, Int32)
public func receive(buffer: Array<Byte>, flags: Int32): Int64
Description: Receives data from the peer end in a connection.
Parameters:
- buffer: Array<Byte>: array for storing received data
- flags: Int32: flag specifying a function behavior
Returns:
- Int64: data length
Throws:
- SocketException: If the current RawSocket instance has been closed or data fails to be received, this exception is thrown.
- SocketTimeoutException: If the specified read timeout is exceeded, this exception is thrown.
func receiveFrom(Array<Byte>, Int32)
public func receiveFrom(buffer: Array<Byte>, flags: Int32): (RawAddress, Int64)
Description: Receives data from another RawSocket instance.
Parameters:
- buffer: Array<Byte>: array for storing received data
- flags: Int32: flag specifying a function behavior
Returns:
- (RawAddress, Int64): data source address and data length
Throws:
- SocketException: If the current RawSocket instance has been closed or data fails to be received, this exception is thrown.
- SocketTimeoutException: If the specified read timeout is exceeded, this exception is thrown.
func send(Array<Byte>, Int32)
public func send(buffer: Array<Byte>, flags: Int32): Unit
Description: Sends data to the peer end in a connection.
Parameters:
Throws:
- SocketException: If the current RawSocket instance has been closed or data fails to be sent, this exception is thrown.
- SocketTimeoutException: If the specified write timeout is exceeded, this exception is thrown.
func sendTo(RawAddress, Array<Byte>, Int32)
public func sendTo(addr: RawAddress, buffer: Array<Byte>, flags: Int32): Unit
Description: Sends data to a destination address. If the type of RawSocket is DATAGRAM
, a data packet to be sent can contain a maximum of 65,507 bytes.
Parameters:
- addr: RawAddress: destination address to which data is sent
- buffer: Array<Byte>: data
- flags: Int32: flag specifying a function behavior
Throws:
- SocketException: If the current RawSocket instance has been closed or data fails to be sent, this exception is thrown.
- SocketTimeoutException: If the specified write timeout is exceeded, this exception is thrown.
- SocketException: On macOS, if
sendTo
is called afterconnect
is called, this exception is thrown.
func setSocketOption(Int32, Int32, CPointer<Byte>, Int32)
public unsafe func setSocketOption(level: Int32, option: Int32, value: CPointer<Byte>, len: Int32): Unit
Description: Sets a socket option.
Parameters:
- level: Int32: socket option level
- option: Int32: socket option name
- value: CPointer<Byte>: socket option value
- len: Int32: socket option value length
Throws:
- SocketException: If the current RawSocket instance has been closed or the socket option fails to be set, this exception is thrown.
class SocketAddress
public open class SocketAddress <: ToString & Hashable & Equatable<SocketAddress> {
public init(kind: SocketAddressKind, address: Array<UInt8>, port: UInt16)
public init(hostAddress: String, port: UInt16)
public init(unixPath!: String)
}
Description: Socket address with specific type, address, and port
Parent Type:
prop address
public prop address: Array<UInt8>
Description: Obtains an address.
prop defaultMask
public prop defaultMask: Option<IPMask>
Description: Obtains the default mask.
prop hostAddress
public prop hostAddress: String
Description: Obtains a host address.
Type: String
prop hostName
public prop hostName: Option<String>
Description: Obtains a name and returns None
if the name does not exist.
prop kind
public prop kind: SocketAddressKind
Description: Obtains an address type.
Type: SocketAddressKind
prop port
public prop port: UInt16
Description: Obtains a port.
Type: UInt16
prop zone
public prop zone: Option<String>
Description: Obtains the IPv6 address family.
init(SocketAddressKind, Array<UInt8>, UInt16)
public init(kind: SocketAddressKind, address: Array<UInt8>, port: UInt16)
Description: Creates a SocketAddress instance based on the input socket address type, IP address, and port number.
Parameters:
- kind: SocketAddressKind: socket address type
- address: Array<UInt8>: IP address
- port: UInt16: port number
Throws:
- SocketException: If the address length does not meet address type requirements, this exception is thrown.
init(String)
public init(unixPath!: String)
Description: Creates a SocketAddress instance based on the input file address.
Parameters:
- unixPath!: String: file address
Throws:
- SocketException: If the address cannot be resolved into the (../../../crypto/x509/x509_package_api/x509_package_type.md#type-ip) format or does not meet address type requirements, this exception is thrown.
init(String, UInt16)
public init(hostAddress: String, port: UInt16)
Description: Creates a SocketAddress instance based on the input IP address and port number.
Parameters:
Throws:
- SocketException: If the address cannot be resolved into the (../../../crypto/x509/x509_package_api/x509_package_type.md#type-ip) format or does not meet address type requirements, this exception is thrown.
static func resolve(String, UInt64)
public static func resolve(domain: String, port: UInt16): ?SocketAddress
Description: Resolves a domain and port, and constructs a SocketAddress instance.
When an IP address is input, the DNS capability is not included. When a domain name address is input, the IP address is obtained through getaddrinfo
resolution. An IPv4 address is preferentially resolved, and only the first IP address obtained is returned.
Parameters:
Returns:
- ?SocketAddress: If the domain and port are resolved successfully, the SocketAddress instance is returned. Otherwise,
None
is returned.
func hashCode()
public open func hashCode(): Int64
Description: Obtains the hashcode
value.
Returns:
- Int64:
hashcode
value
func kapString()
public func kapString(): String
Description: Obtains an IP protocol, address, and port string.
Returns:
Throws:
- SocketException: If the address size does not match IPv4 or IPv6, this exception is thrown.
func mask(IPMask)
public func mask(mask: IPMask): SocketAddressWithMask
Description: Uses the mask to process an address.
Parameters:
- mask: IPMask: mask
Returns:
- SocketAddressWithMask: address after processing
Throws:
- SocketException: If the mask length does not comply with the address length, this exception is thrown.
- IllegalArgumentException: If the buffer space does not meet IPv4 or IPv6 requirements, this exception is thrown.
func setHostName(String)
public func setHostName(name: String): Unit
Description: Sets a host name.
func toString()
public open func toString(): String
Description: Obtains an address object string.
Format: "127.0.0.1:2048", "[::]:2048", "[212c:3742::ffff:5863:6f00]:2048", and the like
Throws:
- SocketException: If the address is invalid, this exception is thrown.
operator func !=(SocketAddress)
public operator func != (that: SocketAddress): Bool
Description: Checks whether two SocketAddress instances are not equal.
Parameters:
- that: SocketAddress: SocketAddress input
Returns:
- Bool: If the two SocketAddress instances are not equal,
true
is returned. Otherwise,false
is returned.
operator func ==(SocketAddress)
public operator func ==(that: SocketAddress): Bool
Description: Checks whether two SocketAddress instances are equal.
Parameters:
- that: SocketAddress: SocketAddress input
Returns:
- Bool: If the two SocketAddress instances are equal,
true
is returned. Otherwise,false
is returned.
class SocketAddressWithMask
public class SocketAddressWithMask <: SocketAddress {
public init(kind: SocketAddressKind, address: Array<UInt8>, port: UInt16, mask: IPMask)
public init(hostAddress: String, port: UInt16, mask: IPMask)
public init(socketAddr: SocketAddress, mask: IPMask)
}
Description: Provides SocketAddress together with a mask.
Parent Type:
prop ipMask
public mut prop ipMask: IPMask
Description: Sets or obtains an IP mask.
Type: IPMask
init(SocketAddress, IPMask)
public init(socketAddr: SocketAddress, mask: IPMask)
Description: Creates a SocketAddressWithMask instance.
Parameters:
- socketAddr: SocketAddress: address
- mask: IPMask: mask
Throws:
- SocketException: If the parameter
socketAddr
is invalid, this exception is thrown.
init(SocketAddressKind, Array<UInt8>, UInt16, IPMask)
public init(kind: SocketAddressKind, address: Array<UInt8>, port: UInt16, mask: IPMask)
Description: Creates a SocketAddressWithMask instance.
Parameters:
- kind: SocketAddressKind: SocketAddress type
- address: Array<UInt8>: address of the type specified by
kind
- port: UInt16: port
- mask: IPMask: mask
Throws:
- SocketException: If the address specified by the parameter
address
does not meet address type requirements of the parameterkind
, this exception is thrown.
init(String, UInt16, IPMask)
public init(hostAddress: String, port: UInt16, mask: IPMask)
Description: Creates a SocketAddressWithMask instance.
Parameters:
Throws:
- SocketException: If the parameter
hostAddress
cannot be resolved into a valid IP address, this exception is thrown.
func clone()
public func clone(): SocketAddressWithMask
Description: Uses the bound mask to process an address and copies a SocketAddressWithMask instance.
Throws:
- SocketException: If the mask length does not comply with the address length, this exception is thrown.
- IllegalArgumentException: If the buffer length is insufficient, this exception is thrown.
func hashCode()
public override func hashCode(): Int64
Description: Obtains the hashcode
value of SocketAddressWithMask.
Returns:
- Int64:
hashcode
value
func toString()
public override func toString(): String
Description: Converts SocketAddressWithMask into a string.
Returns:
- String: string after conversion
class TcpServerSocket
public class TcpServerSocket <: ServerSocket {
public init(bindAt!: SocketAddress)
public init(bindAt!: UInt16)
}
Description: Specifies a server that listens for TCP connections.
After a socket is created, a property value can be configured through prop and the setSocketOptionXX
function.
To start listening, bind()
must be called to bind the socket to a local port. The accept()
function accepts the TCP connection, and the Cangjie thread is blocked until receiving a connection. If a connection already exists in the queue, a response is returned immediately.
The socket can be explicitly closed through close.
Parent Type:
prop backlogSize
public mut prop backlogSize: Int64
Description: Sets and reads the size of backlog
.
If the property is called after bind
is called, an exception is thrown.
Whether a variable takes effect depends on system behaviors.
Type: Int64
Throws:
- SocketException: If the property is called after
bind
is called, this exception is thrown.
prop bindToDevice
public mut prop bindToDevice: ?String
Description: Sets and reads a bound NIC.
Type: ?String
prop localAddress
public override prop localAddress: SocketAddress
Description: Reads the local address of Socket
that is to be or has been bound.
Type: SocketAddress
Throws:
- SocketException: If
Socket
has been closed, this exception is thrown.
prop receiveBufferSize
public mut prop receiveBufferSize: Int64
Description: Sets and reads the SO_RCVBUF
property.
Type: Int64
Throws:
- IllegalArgumentException: If the value of
size
is less than or equal to 0, this exception is thrown. - SocketException: If
Socket
has been closed, this exception is thrown.
prop reuseAddress
public mut prop reuseAddress: Bool
Description: Sets and reads the SO_REUSEADDR
property. The default value is true
.
Behaviors after the property takes effect depend on the system. Before usage, refer to description documents about the SO_REUSEADDR/SOCK_REUSEADDR
property in different systems.
Type: Bool
prop reusePort
public mut prop reusePort: Bool
Description: Sets and reads the SO_REUSEPORT
property.
This property can be modified only before binding. On Windows, the SO_REUSEADDR
property cannot be used because it does not exist, and an exception is thrown.
Behaviors when the default property is used and after configuration takes effect depend on the system. Before usage, refer to description documents about the SO_REUSEPORT
property in different systems.
If both SO_REUSEADDR/SO_REUSEPORT
are enabled, unpredictable system errors occur. Exercise caution during configuration.
Type: Bool
Throws:
- SocketException: On Windows, this property is not supported, and this exception is thrown.
prop sendBufferSize
public mut prop sendBufferSize: Int64
Description: Sets and reads the SO_SNDBUF
property.
Type: Int64
Throws:
- IllegalArgumentException: If the value of
size
is less than or equal to 0, this exception is thrown. - SocketException: If
Socket
has been closed, this exception is thrown.
init(SocketAddress)
public init(bindAt!: SocketAddress)
Description: Creates a TcpServerSocket instance, which cannot be connected by clients before binding is complete.
Parameters:
- bindAt!: SocketAddress: local address to be bound. If the port number is set to 0, a random idle local address is bound.
init(UInt16)
public init(bindAt!: UInt16)
Description: Creates a TcpServerSocket instance, which cannot be connected by clients before binding is complete.
Parameters:
- bindAt!: UInt16: specifies a local port to be bound. The value 0 indicates that a random idle local port is bound.
func accept()
public override func accept(): TcpSocket
Description: Listens for or accepts client connections. After the function is called, the Cangjie thread is blocked until receiving a connection.
Returns:
- TcpSocket: client socket
Throws:
- SocketException: If listening fails due to a system error, this exception is thrown.
func accept(?Duration)
public override func accept(timeout!: ?Duration): TcpSocket
Description: Listens for or accepts client connections.
Parameters:
- timeout!: ?Duration: timeout
Returns:
- TcpSocket: client socket
Throws:
- SocketTimeoutException: If the connection times out, this exception is thrown.
- SocketException: If listening fails due to a system error, this exception is thrown.
- IllegalArgumentException: If the timeout is less than 0, this exception is thrown.
func bind()
public override func bind(): Unit
Description: If a local port fails to be bound, the socket needs to be closed using close
. Retrying for many times is not supported.
Throws:
- SocketException: If binding fails due to a system error, this exception is thrown.
func close()
public override func close(): Unit
Description: Closes a socket. This function can be called more than once.
func getSocketOption(Int32, Int32, CPointer<Unit>, CPointer<UIntNative>)
public func getSocketOption(
level: Int32,
option: Int32,
value: CPointer<Unit>,
valueLength: CPointer<UIntNative>
): Unit
Description: Obtains a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: CPointer<Unit>: parameter value
- valueLength: CPointer<UIntNative>: parameter value length
Throws:
- SocketException: If the
getsockopt
function fails to be called, this exception is thrown.
func getSocketOptionBool(Int32, Int32)
public func getSocketOptionBool(
level: Int32,
option: Int32
): Bool
Description: Obtains a specified socket parameter. It is forcibly converted from IntNative. 0
is converted to false
, and a non-zero value is converted to true
.
Parameters:
Returns:
- Bool: specified socket parameter. It is forcibly converted from IntNative.
0
is converted tofalse
, and a non-zero value is converted totrue
.
Throws:
- SocketException: If the
getsockopt
function fails to be called or the parameter value exceeds the threshold of IntNative, this exception is thrown.
func getSocketOptionIntNative(Int32, Int32)
public func getSocketOptionIntNative(
level: Int32,
option: Int32
): IntNative
Description: Obtains a specified socket parameter.
Parameters:
Returns:
- IntNative: value of the socket parameter obtained
Throws:
- SocketException: If the
getsockopt
function fails to be called or the parameter value exceeds the threshold of IntNative, this exception is thrown.
func isClosed()
public override func isClosed(): Bool
Description: Checks whether a socket has been closed.
Returns:
- Bool: If the socket has been closed,
true
is returned. Otherwise,false
is returned.
func setSocketOption(Int32, Int32, CPointer<Unit>, UIntNative)
public func setSocketOption(
level: Int32,
option: Int32,
value: CPointer<Unit>,
valueLength: UIntNative
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: CPointer<Unit>: parameter value
- valueLength: UIntNative: parameter value length
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func setSocketOptionBool(Int32, Int32, Bool)
public func setSocketOptionBool(
level: Int32,
option: Int32,
value: Bool
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: Bool: parameter value
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func setSocketOptionIntNative(Int32, Int32, IntNative)
public func setSocketOptionIntNative(
level: Int32,
option: Int32,
value: IntNative
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: IntNative: parameter value
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func toString()
public override func toString(): String
Description: Returns status information about the current TcpServerSocket.
Returns:
- String: string containing status information about the current TcpServerSocket
class TcpSocket
public class TcpSocket <: StreamingSocket & Equatable<TcpSocket> & Hashable {
public init(address: String, port: UInt16)
public init(address: SocketAddress)
public init(address: SocketAddress, localAddress!: ?SocketAddress)
}
Description: Serves as a client requesting a TCP connection.
After an instance object is created, a connection can be created using the connect
function and explicitly closed using close
.
This class is inherited from StreamingSocket. For more information, see StreamingSocket.
Parent Type:
prop bindToDevice
public mut prop bindToDevice: ?String
Description: Sets and reads a bound NIC.
Type: ?String
prop keepAlive
public mut prop keepAlive: ?SocketKeepAliveConfig
Description: Sets and reads the keepalive property. None
indicates that keepalive is disabled.
If there is no user setting, the system uses the default configuration. Setting this configuration may be delayed or ignored by the system, which depends on the processing capability of the system.
Type: ?SocketKeepAliveConfig
prop linger
public mut prop linger: ?Duration
Description: Sets and reads the SO_LINGER
property. The default value depends on the system. None
indicates that this option is disabled.
Note:
- If
SO_LINGER
is set toSome(v)
, when the socket is closed, waiting lasts forv
(time) before the connection is closed if there are still byte streams in waiting status. If these byte streams are still not sent after the period, the connection is terminated by an exception (closed through RST packets).- If
SO_LINGER
is set toNone
, when the socket is closed, the connection is closed immediately. If there is no character waiting to be sent, FIN-ACK is used to close the connection. If there are still characters waiting to be sent, RST is used to close the connection.
Type: ?Duration
Throws:
- IllegalArgumentException: If the timeout is less than 0, this exception is thrown.
prop localAddress
public override prop localAddress: SocketAddress
Description: Reads the local address of Socket
that is to be or has been bound.
Type: SocketAddress
Throws:
- SocketException: If
Socket
has been closed or no local address is available (no local address is configured and the socket is not connected), this exception is thrown.
prop noDelay
public mut prop noDelay: Bool
Description: Sets and reads the TCP_NODELAY
property. The default value is true
.
This option disables the Nagel algorithm, and all written bytes are forwarded without delay. When the property is set to false
, the Nagel algorithm introduces a delay time before packet sending.
Type: Bool
prop quickAcknowledge
public mut prop quickAcknowledge: Bool
Description: Sets and reads the TCP_QUICKACK
property. The default value is false
.
This option is similar to noDelay
but affects only TCP ACK and the first response. It is not supported on Windows or macOS.
Type: Bool
prop readTimeout
public override mut prop readTimeout: ?Duration
Description: Sets and reads the read operation timeout.
If the value is too small, the minimum clock cycle is used. If the value is too large, the maximum timeout (263 – 1 nanoseconds) is used. The default value is None
.
Type: ?Duration
Throws:
- IllegalArgumentException: If the timeout is less than 0, this exception is thrown.
prop receiveBufferSize
public mut prop receiveBufferSize: Int64
Description: Sets and reads the SO_RCVBUF
property, and provides a method to specify the size for buffering received packets. Whether the option takes effect depends on the system.
Type: Int64
Throws:
- IllegalArgumentException: If the value of
size
is less than or equal to 0, this exception is thrown. - SocketException: If
Socket
has been closed, this exception is thrown.
prop remoteAddress
public override prop remoteAddress: SocketAddress
Description: Reads the remote address to which Socket
has been or is to be connected.
Type: SocketAddress
Throws:
- SocketException: If
Socket
has been closed, this exception is thrown.
prop sendBufferSize
public mut prop sendBufferSize: Int64
Description: Sets and reads the SO_SNDBUF
property, and provides a method to specify the size for buffering packets to be sent. Whether the option takes effect depends on the system.
Type: Int64
Throws:
- IllegalArgumentException: If the value of
size
is less than or equal to 0, this exception is thrown. - SocketException: If
Socket
has been closed, this exception is thrown.
prop writeTimeout
public override mut prop writeTimeout: ?Duration
Description: Sets and reads the write operation timeout.
If the value is too small, the minimum clock cycle is used. If the value is too large, the maximum timeout (263 – 1 nanoseconds) is used. The default value is None
.
Type: ?Duration
Throws:
- IllegalArgumentException: If the timeout is less than 0, this exception is thrown.
init(SocketAddress)
public init(address: SocketAddress)
Description: Creates a socket that is not connected.
Parameters:
- address: SocketAddress: address to be connected to
Throws:
- SocketException: On Windows, if the
address
parameter is invalid, or the address is an all-zero address, this exception is thrown.
init(SocketAddress, ?SocketAddress)
public init(address: SocketAddress, localAddress!: ?SocketAddress)
Description: Creates a socket that is not connected and binds it to a specified local address. If None
of local addresses are available, an address is randomly selected for the binding.
If localAddress
is not None
, SO_REUSEADDR
is set to true
by default. Otherwise, the error "address already in use" may occur. To change this configuration, call setSocketOptionBool(SocketOptions.SOL_SOCKET, SocketOptions.SO_REUSEADDR, false). In addition, the local and remote addresses must be IPv4 addresses.
Parameters:
- address: SocketAddress: address to be connected to
- localAddress!: ?SocketAddress: local address bound
Throws:
- SocketException: On Windows, if the
address
parameter is invalid, or the address is an all-zero address, this exception is thrown.
init(String, UInt16)
public init(address: String, port: UInt16)
Description: Creates a socket that is not connected.
Parameters:
- address: String: address to be connected to
- port: [UInt16](../../core/core_package_api/core_package_intrinsics.md#uint16: port to be connected to
Throws:
- SocketException: On Windows, if the
address
parameter is invalid, or the address is an all-zero address, this exception is thrown.
func close()
public func close(): Unit
Description: Closes a socket. All operations except close/isClosed
are not allowed to be called again. This function can be called more than once.
func connect(?Duration)
public func connect(timeout!: ?Duration = None): Unit
Description: Connects to a remote socket, with the local address automatically bound. Therefore, no additional binding operation is required.
Parameters:
- timeout!: ?Duration: connection timeout.
None
indicates no timeout and no connection retry. If the server rejects the connection, connection failure is returned. In addition, this operation includes the binding operation. Therefore, repeatedly calling thebind
function is not required.
Throws:
- IllegalArgumentException: If the remote address is invalid, the connection timeout is less than 0, or the timeout is less than 0, this exception is thrown.
- SocketException: If the connection cannot be established due to a system issue (for example, socket closed, no access permission, or system error), this exception is thrown. The connection may be successful after the function is called again.
- SocketTimeoutException: If the connection times out, this exception is thrown.
func getSocketOption(Int32, Int32, CPointer<Unit>, CPointer<UIntNative>)
public func getSocketOption(
level: Int32,
option: Int32,
value: CPointer<Unit>,
valueLength: CPointer<UIntNative>
): Unit
Description: Reads a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: CPointer<Unit>: parameter value
- valueLength: CPointer<UIntNative>: parameter value length
Throws:
- SocketException: If the
getsockopt
function fails to be called, this exception is thrown.
func getSocketOptionBool(Int32, Int32)
public func getSocketOptionBool(
level: Int32,
option: Int32
): Bool
Description: Reads a specified socket parameter. It is forcibly converted from IntNative. 0
is converted to false
, and a non-zero value is converted to true
.
Parameters:
Returns:
- Bool: parameter value read
Throws:
- SocketException: If the
getsockopt
function fails to be called or the parameter value exceeds the threshold of IntNative, this exception is thrown.
func getSocketOptionIntNative(Int32, Int32)
public func getSocketOptionIntNative(
level: Int32,
option: Int32
): IntNative
Description: Reads a specified socket parameter.
Parameters:
Returns:
- IntNative: parameter value
Throws:
- SocketException: If the
getsockopt
function fails to be called or the parameter value exceeds the threshold of IntNative, this exception is thrown.
func hashCode()
public override func hashCode(): Int64
Description: Obtains the hash value of the current TcpSocket instance.
Returns:
func isClosed()
public func isClosed(): Bool
Description: Checks whether a socket has been explicitly closed by calling close
.
Returns:
- Bool: whether the socket has been explicitly closed by calling
close
. If yes,true
is returned. Otherwise,false
is returned.
func read(Array<Byte>)
public override func read(buffer: Array<Byte>): Int64
Description: Reads packets. Timeout is determined by readTimeout
. For details, see readTimeout
.
Note:
- Due to the differences among underlying APIs of the system, if the connection is closed by the peer end, behaviors of the
read
andwrite
functions are also different.- On Windows, after the peer end closes the connection, if
write
is called at the local end, content in the buffer is cleared. In this case, ifread
is called, a connection close exception is thrown.- On Linux or macOS, after the peer end closes the connection, if
read
is called afterwrite
is called, content in the buffer is still read.
Parameters:
Returns:
- Int64: length of the read data
Throws:
- SocketException: If the size of
buffer
is 0 or a read operation fails due to a system error, this exception is thrown.
func setSocketOption(Int32, Int32, CPointer<Unit>, UIntNative)
public func setSocketOption(
level: Int32,
option: Int32,
value: CPointer<Unit>,
valueLength: UIntNative
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: CPointer<Unit>: parameter value
- valueLength: UIntNative: parameter value length
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func setSocketOptionBool(Int32, Int32, Bool)
public func setSocketOptionBool(
level: Int32,
option: Int32,
value: Bool
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: Bool: parameter value
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func setSocketOptionIntNative(Int32, Int32, IntNative)
public func setSocketOptionIntNative(
level: Int32,
option: Int32,
value: IntNative
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: IntNative: parameter value
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func toString()
public override func toString(): String
Description: Returns status information about the current TcpSocket.
Returns:
func write(Array<Byte>)
public override func write(payload: Array<Byte>): Unit
Description: Writes packets. Timeout is determined by writeTimeout
. For details, see writeTimeout
.
Parameters:
Throws:
- SocketException: If the size of
buffer
is 0 or a write operation fails due to a system error, this exception is thrown.
operator func !=(TcpSocket)
public override operator func !=(other: TcpSocket): Bool
Description: Checks whether two TcpSocket instances are not equal.
Parameters:
Returns:
operator func ==(TcpSocket)
public override operator func ==(other: TcpSocket): Bool
Description: Checks whether two TcpSocket instances are equal.
Parameters:
Returns:
class UdpSocket
public class UdpSocket <: DatagramSocket {
public init(bindAt!: SocketAddress)
public init(bindAt!: UInt16)
}
Description: Provides UDP packet communication.
After a UDPSocket
instance is created, bind()
needs to be called for binding. Packets can be received without connection to the remote end. In addition, connection can be established for UDPSocket
through the connect()/disconnect()
function.
Packets to be transmitted cannot exceed 64 KB, which is required by the UDP protocol.
UDPSocket
needs to be explicitly closed using close()
. For more information, see DatagramSocket.
Parent Type:
prop localAddress
public override prop localAddress: SocketAddress
Description: Reads the local address of Socket
that is to be or has been bound.
Type: SocketAddress
Throws:
- SocketException: If
Socket
has been closed or no local address is available (no local address is configured and the socket is not connected), this exception is thrown.
prop receiveBufferSize
public mut prop receiveBufferSize: Int64
Description: Sets and reads the SO_RCVBUF
property.
Type: Int64
Throws:
- IllegalArgumentException: If the value of
size
is less than or equal to 0, this exception is thrown. - SocketException: If
Socket
has been closed, this exception is thrown.
prop receiveTimeout
public override mut prop receiveTimeout: ?Duration
Description: Sets and reads the receive/receiveFrom
operation timeout.
If the value is too small, the minimum clock cycle is used. If the value is too large, the maximum timeout (263 – 1 nanoseconds) is used. The default value is None
.
Type: ?Duration
Throws:
- IllegalArgumentException: If the timeout is less than 0, this exception is thrown.
prop remoteAddress
public override prop remoteAddress: ?SocketAddress
Description: Reads the remote address connected to Socket
. If Socket
is not connected, None
is returned.
Type: ?SocketAddress
Throws:
- SocketException: If
Socket
has been closed, this exception is thrown.
prop reuseAddress
public mut prop reuseAddress: Bool
Description: Sets and reads the SO_REUSEADDR
property.
Behaviors when the default property is used and after it takes effect depend on the system. Before usage, refer to description documents about the SO_REUSEADDR/SOCK_REUSEADDR
property in different systems.
Type: Bool
prop reusePort
public mut prop reusePort: Bool
Description: Sets and reads the SO_REUSEPORT
property.
On Windows, SO_REUSEADDR
can be used but the SO_REUSEPORT
property is excluded. Therefore, an exception is thrown.
Behaviors when the default property is used and after configuration takes effect depend on the system. Before usage, refer to description documents about the SO_REUSEPORT
property in different systems.
Type: Bool
Throws:
- SocketException: On Windows, this property is not supported, and this exception is thrown.
prop sendBufferSize
public mut prop sendBufferSize: Int64
Description: Sets and reads the SO_SNDBUF
property.
Type: Int64
Throws:
- IllegalArgumentException: If the value of
size
is less than or equal to 0, this exception is thrown. - SocketException: If
Socket
has been closed, this exception is thrown.
prop sendTimeout
public override mut prop sendTimeout: ?Duration
Description: Sets and reads the send/sendTo
operation timeout.
If the value is too small, the minimum clock cycle is used. If the value is too large, the maximum timeout (263 – 1 nanoseconds) is used. The default value is None
.
Type: ?Duration
init(SocketAddress)
public init(bindAt!: SocketAddress)
Description: Creates an unbound UDPSocket
instance.
Parameters:
- bindAt!: SocketAddress: address and port to be bound
Throws:
- IllegalArgumentException: If the timeout is less than 0, this exception is thrown.
init(UInt64)
public init(bindAt!: UInt16)
Description: Creates an unbound UDPSocket
instance.
Parameters:
- bindAt!: UInt16: port to be bound
func bind()
public func bind(): Unit
Description: If a local port fails to be bound, the socket needs to be closed using close
. Retrying for many times is not supported.
Throws:
- SocketException: If binding fails due to a system error, this exception is thrown.
func close()
public override func close(): Unit
Description: Closes a socket. All operations except close/isClosed
are not allowed to be called again. This function can be called more than once.
func connect(SocketAddress)
public func connect(remote: SocketAddress): Unit
Description: Connects to a specified remote address. The configuration can be canceled using disconnect
.
Only packets from the remote address are received. This operation must be performed after bind
is called. After this operation is performed, the port starts to receive ICMP packets. If abnormal packets are received, send/sendTo
may fail to be executed.
Parameters:
- remote: SocketAddress: remote address
Throws:
- IllegalArgumentException: If the remote address is invalid, this exception is thrown.
- SocketException: On Windows, if the port is not bound, the connection cannot be established due to a system error, or the remote address is an all-zero address, this exception is thrown.
func disconnect()
public func disconnect(): Unit
Description: Stops a connection, which cancels the configuration that only packets from a specified peer end are received. This function can be called before connect
more than once.
func getSocketOption(Int32, Int32, CPointer<Unit>, CPointer<UIntNative>)
public func getSocketOption(
level: Int32,
option: Int32,
value: CPointer<Unit>,
valueLength: CPointer<UIntNative>
): Unit
Description: Obtains a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: CPointer<Unit>: parameter value
- valueLength: CPointer<UIntNative>: parameter value length
Throws:
- SocketException: If the
getsockopt
function fails to be called, this exception is thrown.
func getSocketOptionBool(Int32, Int32)
public func getSocketOptionBool(
level: Int32,
option: Int32
): Bool
Description: Obtains a specified socket parameter. It is forcibly converted from IntNative. 0
is converted to false
, and a non-zero value is converted to true
.
Parameters:
Returns:
- Bool: specified socket parameter. It is forcibly converted from IntNative.
0
is converted tofalse
, and a non-zero value is converted totrue
.
Throws:
- SocketException: If the
getsockopt
function fails to be called or the parameter value exceeds the threshold of IntNative, this exception is thrown.
func getSocketOptionIntNative(Int32, Int32)
public func getSocketOptionIntNative(
level: Int32,
option: Int32
): IntNative
Description: Obtains a specified socket parameter.
Parameters:
Returns:
- IntNative: value of the specified socket parameter
Throws:
- SocketException: If the
getsockopt
function fails to be called or the parameter value exceeds the threshold of IntNative, this exception is thrown.
func isClosed()
public override func isClosed(): Bool
Description: Checks whether a socket has been explicitly closed by calling close
.
Returns:
- Bool: If the socket has been explicitly closed using
close
,true
is returned. Otherwise,false
is returned.
func receive(Array<Byte>)
public func receive(buffer: Array<Byte>): Int64
Description: Receives packets from an address connected to using connect
.
Parameters:
Returns:
- [Int64](../../core/core_package_api/core_package_intrinsics.md#int64: size of the received packets
func receiveFrom(Array<Byte>)
public override func receiveFrom(buffer: Array<Byte>): (SocketAddress, Int64)
Description: Receives packets.
Parameters:
Returns:
- (SocketAddress, Int64): address from which the received packets are sent, and size of the received packets, 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 send(Array<Byte>)
public func send(payload: Array<Byte>): Unit
Description: Sends packets to the address connected to using connect
.
Parameters:
Throws:
- SocketException: If the size of
payload
exceeds the system limit, the system fails to send the packets because, for example,connect
is called, and receives abnormal ICMP packets, this exception is thrown.
func sendTo(SocketAddress, Array<Byte>)
public override func sendTo(recipient: SocketAddress, payload: Array<Byte>): Unit
Description: Sends packets. Insufficient buffer addresses may cause blocking.
Parameters:
- recipient: SocketAddress: peer address to which the packets are sent
- payload: Array<Byte>: content of the sent packets
Throws:
- SocketException: If the size of
payload
exceeds the system limit, the system fails to send the packets because (For example, whenconnect
is called and abnormal ICMP packets are received, the sending fails), the remote address is an all-zero address, orsendTo
is called afterconnect
is called, this exception is thrown.
func setSocketOption(Int32, Int32, CPointer<Unit>, UIntNative)
public func setSocketOption(
level: Int32,
option: Int32,
value: CPointer<Unit>,
valueLength: UIntNative
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: CPointer<Unit>: parameter value
- valueLength: UIntNative: parameter value length
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func setSocketOptionBool(Int32, Int32, Bool)
public func setSocketOptionBool(
level: Int32,
option: Int32,
value: Bool
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: Bool: parameter value
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func setSocketOptionIntNative(Int32, Int32, IntNative)
public func setSocketOptionIntNative(
level: Int32,
option: Int32,
value: IntNative
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: IntNative: parameter value
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func toString()
public override func toString(): String
Description: Returns status information about the current UdpSocket.
Returns:
class UnixDatagramSocket
public class UnixDatagramSocket <: DatagramSocket {
public init(bindAt!: SocketAddress)
public init(bindAt!: String)
}
Description: Provides the host communication capability based on data packets.
After the UnixDatagramSocket instance is created, the bind()
function should be explicitly called for binding. For Unix
data packets, the socket does not require a connection or many handshakes with the remote end. However, users can also use the connect/disconnect
function to connect to or disconnect from the remote end.
Different from UDP, UDS has no restriction on the data packet size. However, there are operating system and API implementation restrictions.
Socket resources need to be explicitly reclaimed using the close
function. For more information, see DatagramSocket.
Note:
- This class is not supported on Windows.
Parent Type:
prop localAddress
public override prop localAddress: SocketAddress
Description: Reads the local address of socket
that is to be or has been bound.
Type: SocketAddress
Throws:
- SocketException: If
socket
has been closed, this exception is thrown.
prop receiveBufferSize
public mut prop receiveBufferSize: Int64
Description: Sets and reads the SO_RCVBUF
property, and provides a method to specify the size for buffering packets to be sent. Whether the option takes effect depends on the system.
Type: Int64
Throws:
- IllegalArgumentException: If the value of
size
is less than or equal to 0, this exception is thrown. - SocketException: If
Socket
has been closed, this exception is thrown.
prop receiveTimeout
public override mut prop receiveTimeout: ?Duration
Description: Sets and reads the receive/receiveFrom
operation timeout.
If the value is too small, the minimum clock cycle is used. If the value is too large, the maximum timeout (263 – 1 nanoseconds) is used. The default value is None
.
Type: ?Duration
Throws:
- IllegalArgumentException: If the timeout is less than 0, this exception is thrown.
prop remoteAddress
public override prop remoteAddress: ?SocketAddress
Description: Reads the remote address connected to Socket
. If Socket
is not connected, None
is returned.
Type: ?SocketAddress
Throws:
- SocketException: If
Socket
has been closed, this exception is thrown.
prop sendBufferSize
public mut prop sendBufferSize: Int64
Description: Sets and reads the SO_SNDBUF
property, and provides a method to specify the size for buffering packets to be sent. Whether the option takes effect depends on the system.
Type: Int64
Throws:
- IllegalArgumentException: If the value of
size
is less than or equal to 0, this exception is thrown. - SocketException: If
Socket
has been closed, this exception is thrown.
prop sendTimeout
public override mut prop sendTimeout: ?Duration
Description: Sets and reads the send/sendTo
operation timeout.
If the value is too small, the minimum clock cycle is used. If the value is too large, the maximum timeout (263 – 1 nanoseconds) is used. The default value is None
.
Type: ?Duration
Throws:
- IllegalArgumentException: If the timeout is less than 0, this exception is thrown.
init(SocketAddress)
public init(bindAt!: SocketAddress)
Description: Creates a UnixDatagramSocket instance that is not connected.
isSock() can be used to check whether the file type exists, and the unlink() function can be used to delete the file type.
Parameters:
- bindAt!: SocketAddress: socket address to be connected to. The address does not exist and will be created upon binding using
bind
.
Throws:
- SocketException: If the path is empty or already exists, this exception is thrown.
init(String)
public init(bindAt!: String)
Description: Creates a UnixDatagramSocket instance that is not connected.
isSock() can be used to check whether the file type exists, and the unlink() function can be used to delete the file type.
Parameters:
- bindAt!: String: file address. The file address does not exist and will be created upon binding using
bind
.
Throws:
- SocketException: If the path is empty or already exists, this exception is thrown.
func bind()
public func bind(): Unit
Description: Binds a Unix datagram
socket and creates a listening queue.
This function automatically creates a socket file in the local address. If the file already exists, the binding fails. isSock can be used to check whether the file type exists, and the unlink() function can be used to delete the file type. Upon failure, the socket needs to be closed using close
. Repeated retries are not supported.
Throws:
- SocketException: If the file address already exists or the file fails to be created, this exception is thrown.
func close()
public override func close(): Unit
Description: Closes a socket. All operations except close/isClosed
are not allowed to be called again. This function can be called more than once.
func connect(SocketAddress)
public func connect(remote: SocketAddress): Unit
Description: Connects to a specified remote address. The configuration can be canceled using disconnect
.
Only packets from the remote address are received. bind
is executed by default without the need of being called. After this operation is performed, the port starts to receive ICMP packets. If abnormal packets are received, send/sendTo
may fail to be executed.
Parameters:
- remote: SocketAddress: remote socket address
Throws:
- SocketException: If the address is not bound, this exception is thrown.
func connect(String)
public func connect(remotePath: String): Unit
Description: Connects to a specified remote address. The configuration can be canceled using disconnect
.
Only packets from the remote address are received. This function must be called after bind
. After this operation is performed, the port starts to receive ICMP packets. If abnormal packets are received, send/sendTo
may fail to be executed.
Parameters:
- remotePath: String: remote file address
Throws:
- SocketException: If the address is not bound, this exception is thrown.
func disconnect()
public func disconnect(): Unit
Description: Stops a connection, which cancels the configuration that only packets from a specified peer end are received. This function can be called before connect
more than once.
Throws:
- SocketException: If binding is not complete, this exception is thrown.
func getSocketOption(Int32, Int32, CPointer<Unit>, CPointer<UIntNative>)
public func getSocketOption(
level: Int32,
option: Int32,
value: CPointer<Unit>,
valueLength: CPointer<UIntNative>
): Unit
Description: Obtains a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: CPointer<Unit>: parameter value
- valueLength: CPointer<UIntNative>: parameter value length
Throws:
- SocketException: If the
getsockopt
function fails to be called, this exception is thrown.
func getSocketOptionIntNative(Int32, Int32)
public func getSocketOptionIntNative(
level: Int32,
option: Int32
): IntNative
Description: Obtains a specified socket parameter.
Parameters:
Returns:
- IntNative: The value of the specified socket parameter is returned.
Throws:
- SocketException: If the
getsockopt
function fails to be called or the parameter value exceeds the threshold of IntNative, this exception is thrown.
func isClosed()
public override func isClosed(): Bool
Description: Checks whether a socket has been explicitly closed by calling close
.
Returns:
- Bool: Information that whether the socket has been explicitly closed by calling
close
is returned. If yes,true
is returned. Otherwise,false
is returned.
func receive(Array<Byte>)
public func receive(buffer: Array<Byte>): Int64
Description: Receives packets from an address connected to using connect
.
Parameters:
Returns:
- [Int64](../../core/core_package_api/core_package_intrinsics.md#int64: size of the received packets
func receiveFrom(Array<Byte>)
public override func receiveFrom(buffer: Array<Byte>): (SocketAddress, Int64)
Description: Receives packets.
Parameters:
Returns:
- (SocketAddress, Int64): address from which the received packets are sent, and size of the received packets, 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 send(Array<Byte>)
public func send(payload: Array<Byte>): Unit
Description: Sends packets to the address connected to using connect
.
Parameters:
Throws:
- SocketException: If the size of
payload
exceeds the system limit, or the system fails to send the packets because, for example,connect
is called, and receives abnormal ICMP packets, the sending fails.
func sendTo(SocketAddress, Array<Byte>)
public override func sendTo(recipient: SocketAddress, payload: Array<Byte>): Unit
Description: Sends packets. Insufficient buffer addresses may cause blocking.
Parameters:
- recipient: SocketAddress: address of the peer end to which the packets are sent
- payload: Array<Byte>: content of the sent packets
Throws:
- SocketException: If the size of
payload
exceeds the system limit, the system fails to send the packets because (For example,connect
is called, and receives abnormal ICMP packets, the sending fails), orsendTo
is called afterconnect
is called, this exception is thrown.
func setSocketOption(Int32, Int32, CPointer<Unit>, UIntNative)
public func setSocketOption(
level: Int32,
option: Int32,
value: CPointer<Unit>,
valueLength: UIntNative
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: CPointer<Unit>: parameter value
- valueLength: UIntNative: parameter value length
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func setSocketOptionBool(Int32, Int32, Bool)
public func setSocketOptionBool(
level: Int32,
option: Int32,
value: Bool
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: Bool: parameter value
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func setSocketOptionIntNative(Int32, Int32, IntNative)
public func setSocketOptionIntNative(
level: Int32,
option: Int32,
value: IntNative
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: IntNative: parameter value
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func getSocketOptionBool(Int32, Int32)
public func getSocketOptionBool(
level: Int32,
option: Int32
): Bool
Description: Obtains a specified socket parameter. It is forcibly converted from IntNative. 0
is converted to false
, and a non-zero value is converted to true
.
Parameters:
Returns:
- Bool: The value of the specified socket parameter is returned. It is forcibly converted from IntNative.
0
is converted tofalse
, and a non-zero value is converted totrue
.
Throws:
- SocketException: If the
getsockopt
function fails to be called, this exception is thrown.
func toString()
public override func toString(): String
Description: Returns status information about the current UDS
.
Returns:
- String: string containing status information about the current
UDS
class UnixServerSocket
public class UnixServerSocket <: ServerSocket
Description: Provides a host communication server based on the duplex stream.
UnixServerSocket listens for connection requests. After creation, a property value can be configured through prop and the setSocketOptionXX
function. The bind()
function needs to be called for binding a local address to start listening for connections. A connection can be accepted through the accept()
function.
Note:
- This class is not supported on Windows.
Parent Type:
prop backlogSize
public mut prop backlogSize: Int64
Description: Sets and reads the size of backlog
. If the property is called after bind
is called, an exception is thrown.
Whether a variable takes effect depends on system behaviors.
Type: Int64
Throws:
- SocketException: If the property is called after
bind
is called, this exception is thrown.
prop localAddress
public override prop localAddress: SocketAddress
Description: Reads the local address of Socket
that is to be or has been bound.
Type: SocketAddress
Throws:
- SocketException: If
Socket
has been closed, this exception is thrown.
prop receiveBufferSize
public mut prop receiveBufferSize: Int64
Description: Sets and reads the SO_RCVBUF
property.
Type: Int64
Throws:
- IllegalArgumentException: If the value of
size
is less than or equal to 0, this exception is thrown. - SocketException: If
Socket
has been closed, this exception is thrown.
prop sendBufferSize
public mut prop sendBufferSize: Int64
Description: Sets and reads the SO_SNDBUF
property.
Type: Int64
Throws:
- IllegalArgumentException: If the value of
size
is less than or equal to 0, this exception is thrown. - SocketException: If
Socket
has been closed, this exception is thrown.
init(SocketAddress)
public init(bindAt!: SocketAddress)
Description: Creates a UnixServerSocket instance that is not connected.
Parameters:
- bindAt!: SocketAddress: socket address to be connected to
init(String)
public init(bindAt!: String)
Description: Creates a UnixServerSocket instance that is not connected.
isSock can be used to check whether the file type exists, and the unlink() function can be used to delete the file type.
Parameters:
- bindAt!: String: file address
func accept()
public override func accept(): UnixSocket
Description: Waits to accept a connection from a client or reads a connection from the queue.
Returns:
- UnixSocket: socket of the client to be connected to
func accept(?Duration)
public override func accept(timeout!: ?Duration): UnixSocket
Description: Waits to accept a connection from a client or reads a connection from the queue.
Parameters:
- timeout!: ?Duration: timeout
Returns:
- UnixSocket: socket of the client to be connected to
Throws:
- SocketTimeoutException: If the connection times out, this exception is thrown.
- IllegalArgumentException: If the timeout is less than 0, this exception is thrown.
func bind()
public override func bind(): Unit
Description: Binds a Unix domain
socket and creates a listening queue.
This function automatically creates a socket file in the local address. If the file already exists, the binding fails. The isSock function can be used to check whether the file type exists, and the unlink() function can be used to delete the file type. Upon failure, the socket needs to be closed using close
. Repeated retries are not supported.
Throws:
- SocketException: If binding fails due to a system error, this exception is thrown.
func close()
public override func close(): Unit
Description: Closes a socket. All operations on the socket except close/isClosed
are not allowed to be called again. This function can be called more than once.
func getSocketOption(Int32, Int32, CPointer<Unit>, CPointer<UIntNative>)
public func getSocketOption(
level: Int32,
option: Int32,
value: CPointer<Unit>,
valueLength: CPointer<UIntNative>
): Unit
Description: Obtains a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: CPointer<Unit>: parameter value
- valueLength: CPointer<UIntNative>: parameter value length
Throws:
- SocketException: If the
getsockopt
function fails to be called, this exception is thrown.
func getSocketOptionBool(Int32, Int32)
public func getSocketOptionBool(
level: Int32,
option: Int32
): Bool
Description: Obtains a specified socket parameter. It is forcibly converted from IntNative. 0
is converted to false
, and a non-zero value is converted to true
.
Parameters:
Returns:
- Bool: The specified socket parameter is returned. It is forcibly converted from IntNative.
0
is converted tofalse
, and a non-zero value is converted totrue
.
Throws:
- SocketException: If the
getsockopt
function fails to be called or the parameter value exceeds the threshold of IntNative, this exception is thrown.
func getSocketOptionIntNative(Int32, Int32)
public func getSocketOptionIntNative(
level: Int32,
option: Int32
): IntNative
Description: Obtains a socket parameter whose return value is an integer.
Parameters:
Returns:
- IntNative: value of the specified socket parameter
Throws:
- SocketException: If the
getsockopt
function fails to be called or the parameter value exceeds the threshold of IntNative, this exception is thrown.
func isClosed()
public override func isClosed(): Bool
Description: Checks whether a socket has been explicitly closed by calling close
.
func setSocketOption(Int32, Int32, CPointer<Unit>, UIntNative)
public func setSocketOption(
level: Int32,
option: Int32,
value: CPointer<Unit>,
valueLength: UIntNative
): Unit
Description: Sets a socket parameter whose return value is an integer.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: CPointer<Unit>: parameter value
- valueLength: UIntNative: parameter value length
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func setSocketOptionBool(Int32, Int32, Bool)
public func setSocketOptionBool(
level: Int32,
option: Int32,
value: Bool
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: Bool: parameter value
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func setSocketOptionIntNative(Int32, Int32, IntNative)
public func setSocketOptionIntNative(
level: Int32,
option: Int32,
value: IntNative
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: IntNative: parameter value
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func toString()
public override func toString(): String
Description: Returns status information about the current UnixServerSocket.
Returns:
- String: string containing status information about the current UnixServerSocket
class UnixSocket
public class UnixSocket <: StreamingSocket {
public init(address: SocketAddress)
public init(path: String)
}
Description: Provides a host communication client based on the duplex stream.
After the UnixSocket instance is created, the connect()
function needs to be called to create a connection, and close()
needs to be explicitly called to reclaim resources when the connection is no more required. For more information, see StreamingSocket.
Note:
- This class is not supported on Windows.
Parent Type:
prop localAddress
public override prop localAddress: SocketAddress
Description: Reads the local address of Socket
that is to be or has been bound.
Type: SocketAddress
Throws:
- SocketException: If
Socket
has 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
public override mut prop readTimeout: ?Duration
Description: Sets and reads the read operation timeout.
If the value is too small, the minimum clock cycle is used. If the value is too large, None
is used. The default value is None
.
Type: ?Duration
Throws:
- IllegalArgumentException: If the timeout is less than 0, this exception is thrown.
prop receiveBufferSize
public mut prop receiveBufferSize: Int64
Description: Sets and reads the SO_RCVBUF
property.
Type: Int64
Throws:
- IllegalArgumentException: If the value of
size
is less than or equal to 0, this exception is thrown. - SocketException: If
Socket
has been closed, this exception is thrown.
prop remoteAddress
public override prop remoteAddress: SocketAddress
Description: Reads the remote address to which Socket
has been or is to be connected.
Type: SocketAddress
Throws:
- SocketException: If
Socket
has been closed, this exception is thrown.
prop sendBufferSize
public mut prop sendBufferSize: Int64
Description: Sets and reads the SO_SNDBUF
property.
Type: Int64
Throws:
- IllegalArgumentException: If the value of
size
is less than or equal to 0, this exception is thrown. - SocketException: If
Socket
has been closed, this exception is thrown.
prop writeTimeout
public override mut prop writeTimeout: ?Duration
Description: Sets and reads the write operation timeout.
If the value is too small, the minimum clock cycle is used. If the value is too large, the maximum timeout (263 – 1 nanoseconds) is used. The default value is None
.
Type: ?Duration
Throws:
- IllegalArgumentException: If the timeout is less than 0, this exception is thrown.
init(SocketAddress)
public init(address: SocketAddress)
Description: Creates a UnixSocket instance that is not connected.
Parameters:
- address: SocketAddress: socket address to be connected to
init(String)
public init(path: String)
Description: Creates a UnixSocket instance that is not connected.
isSock can be used to check whether the file type exists, and the unlink() function can be used to delete the file type.
Parameters:
- path: String: file address
func close()
public func close(): Unit
Description: Closes a socket. All operations except close/isClosed
are not allowed to be called again. This function can be called more than once.
func connect(?Duration)
public func connect(timeout!: ?Duration = None): Unit
Description: Establishes a connection with the peer end. If the peer end rejects the connection, the connection fails and a local address is automatically bound. Therefore, no additional binding operation is required.
Parameters:
- timeout!: ?Duration: timeout.
None
indicates no timeout. Different from TCP, when Unix is used, if the queue is full, an error is returned immediately without retrying blocked waiting when the function is called.
Throws:
- IllegalArgumentException: If the remote address is invalid, or the timeout is less than 0, this exception is thrown.
- SocketException: If the connection cannot be established due to a system error, this exception is thrown.
- SocketTimeoutException: If the connection times out, this exception is thrown.
func getSocketOption(Int32, Int32, CPointer<Unit>, CPointer<UIntNative>)
public func getSocketOption(
level: Int32,
option: Int32,
value: CPointer<Unit>,
valueLength: CPointer<UIntNative>
): Unit
Description: Obtains a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: CPointer<Unit>: parameter value
- valueLength: CPointer<UIntNative>: parameter value length
Throws:
- SocketException: If the
getsockopt
function fails to be called, this exception is thrown.
func getSocketOptionBool(Int32, Int32)
public func getSocketOptionBool(
level: Int32,
option: Int32
): Bool
Description: Obtains a specified socket parameter. It is forcibly converted from IntNative. 0
is converted to false
, and a non-zero value is converted to true
.
Parameters:
Returns:
- Bool: The value of the specified socket parameter is returned. It is forcibly converted from IntNative.
0
is converted tofalse
, and a non-zero value is converted totrue
.
Throws:
- SocketException: If the
getsockopt
function fails to be called or the parameter value exceeds the threshold of IntNative, this exception is thrown.
func getSocketOptionIntNative(Int32, Int32)
public func getSocketOptionIntNative(
level: Int32,
option: Int32
): IntNative
Description: Obtains a specified socket parameter.
Parameters:
Returns:
- IntNative: value of the specified socket parameter
Throws:
- SocketException: If the
getsockopt
function fails to be called or the parameter value exceeds the threshold of IntNative, this exception is thrown.
func isClosed()
public func isClosed(): Bool
Description: Checks whether a socket has been explicitly closed by calling close
.
Returns:
- Bool: Information that whether the socket has been explicitly closed by calling
close
is returned. If yes,true
is returned. Otherwise,false
is returned.
func read(Array<Byte>)
public override func read(buffer: Array<Byte>): Int64
Description: Reads packets. Timeout is determined by readTimeout
. For details, see readTimeout
.
Parameters:
Returns:
- Int64: length of the read data
Throws:
- SocketException: If the size of
buffer
is 0 or a read operation fails due to a system error, this exception is thrown.
func setSocketOption(Int32, Int32, CPointer<Unit>, UIntNative)
public func setSocketOption(
level: Int32,
option: Int32,
value: CPointer<Unit>,
valueLength: UIntNative
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: CPointer<Unit>: parameter value
- valueLength: UIntNative: parameter value length
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func setSocketOptionBool(Int32, Int32, Bool)
public func setSocketOptionBool(
level: Int32,
option: Int32,
value: Bool
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: Bool: parameter value
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func setSocketOptionIntNative(Int32, Int32, IntNative)
public func setSocketOptionIntNative(
level: Int32,
option: Int32,
value: IntNative
): Unit
Description: Sets a specified socket parameter.
Parameters:
- level: Int32: level, for example,
SOL_SOCKET
- option: Int32: parameter, for example,
SO_KEEPALIVE
- value: IntNative: parameter value
Throws:
- SocketException: If the
setsockopt
function fails to be called, this exception is thrown.
func toString()
public override func toString(): String
Description: Returns status information about the current UnixSocket.
Returns:
- String: string containing status information about the current UnixSocket
func write(Array<Byte>)
public override func write(buffer: Array<Byte>): Unit
Description: Reads and writes data. Timeout is determined by writeTimeout
. For details, see writeTimeout
.
Parameters:
Throws:
- SocketException: If the size of
buffer
is 0 or a write operation fails due to a system error, this exception is thrown.