Socket
public struct Socket: FileDescriptor
Socket is an endpoint for network communication. See socket(2).
-
The file descriptor for this socket.
Declaration
Swift
public let fileDescriptor: Int32
-
The network family of this socket. Can be nil if the socket was created from an existing file descriptor and the current platform does not support looking up the family.
Declaration
Swift
public let family: Family?
-
The socket protocol.
Declaration
Swift
public let proto: SocketProtocol
-
The socket type.
Declaration
Swift
public let type: SocketType
-
Creates an unconnected socket with the specified family and type, automatically deducing the protocol (unix if family is unix, otherwise TCP if the type is stream, UDP if the type is datagram).
Declaration
Swift
public init(family: Family = .inet, type: SocketType = .stream) throws
-
Creates a socket using an already existing socket’s file descriptor. The family can be specified if known, otherwise it will attempt to detect it if supported by the platform.
Declaration
Swift
public init(fd: Int32, family: Family? = nil) throws
-
Sets an Int32-based option on the socket. Boolean options are set by passing 1 for true, 0 for false.
Declaration
Swift
public func setOption(_ option: Int32, to value: Int) throws
-
Gets the current setting for an Int32-based option.
Declaration
Swift
public func getOption(_ option: Int32) throws -> Int
-
Sets the read timeout option.
Declaration
Swift
public func setReadTimeout(_ t: TimeInterval) throws
-
Gets the current read timeout setting.
Declaration
Swift
public func getReadTimeout() throws -> TimeInterval
-
Sets the write timeout option.
Declaration
Swift
public func setWriteTimeout(_ t: TimeInterval) throws
-
Gets the current write timeout setting.
Declaration
Swift
public func getWriteTimeout() throws -> TimeInterval
-
Sets the linger option.
Declaration
Swift
public func setLinger(timeout: TimeInterval?) throws
-
Gets the current linger setting.
Declaration
Swift
public func getLinger() throws -> TimeInterval?
-
Sets the socket as blocking.
Declaration
Swift
public func setBlocking() throws
-
Sets the socket as non-blocking.
Declaration
Swift
public func setNonBlocking() throws
-
Indicates if the socket is currently set as blocking.
Declaration
Swift
public func isBlocking() throws -> Bool
-
Returns the bound address of the socket.
Declaration
Swift
public func boundAddress() throws -> Address
-
Returns the peer address of the socket.
Declaration
Swift
public func peerAddress() throws -> Address
-
Binds the socket to the specified address.
Declaration
Swift
public func bind(to addr: Address) throws
-
Connects the socket to the specified address.
Declaration
Swift
public func connect(to addr: Address) throws
-
Sends data over the socket.
Declaration
Swift
public func send(_ data: Array<UInt8>, flags: SendFlags = []) throws -> Int
-
Sends data over the socket.
Declaration
Swift
public func send(_ data: ArraySlice<UInt8>, flags: SendFlags = []) throws -> Int
-
Read data from the socket into the provided array. At most
data.count
bytes are read.Declaration
Swift
public func receive(_ data: inout Array<UInt8>, flags: ReceiveFlags = []) throws -> Int
-
Read data from the socket into the provided array. At most
data.count
bytes are read.Declaration
Swift
public func receive(_ data: inout ArraySlice<UInt8>, flags: ReceiveFlags = []) throws -> Int
-
Read data from the socket into the provided array and stores the sender address in
addr
. At mostdata.count
bytes are read.Declaration
Swift
public func receive(_ data: inout Array<UInt8>, from addr: inout Address, flags: ReceiveFlags = []) throws -> Int
-
Read data from the socket into the provided array and stores the sender address in
addr
. At mostdata.count
bytes are read.Declaration
Swift
public func receive(_ data: inout ArraySlice<UInt8>, from addr: inout Address, flags: ReceiveFlags = []) throws -> Int
-
Listen for incoming connections on the bound address.
Declaration
Swift
public func listen(backlog: Int = 128) throws
-
Accept an incoming connection, returning the accepted socket.
Declaration
Swift
public func accept() throws -> Socket
-
Shutdown the read, write or both ends of the socket. The socket must still be closed to properly release all resources.
Declaration
Swift
public func shutdown(mode: ShutdownMode = .readWrite) throws
-
Releases the resources for this file descriptor.
Declaration
Swift
public func close() throws
-
Available socket shutdown modes.
See moreDeclaration
Swift
public enum ShutdownMode