Socket

Class that creates a network communication endpoint using the Berkeley sockets interface.

@safe
class Socket {}

Constructors

this
this(AddressFamily af, SocketType type, ProtocolType protocol)
this(AddressFamily af, SocketType type)
this(AddressFamily af, SocketType type, const(char)[] protocolName)

Create a blocking socket. If a single protocol type exists to support this socket type within the address family, the ProtocolType may be omitted.

this
this(AddressInfo info)

Create a blocking socket using the parameters from the specified AddressInfo structure.

this
this(socket_t sock, AddressFamily af)

Use an existing socket handle.

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Members

Functions

accept
Socket accept()

Accept an incoming connection. If the socket is blocking, accept waits for a connection request. Throws SocketAcceptException if unable to accept. See accepting for use with derived classes.

accepting
Socket accepting()

Called by accept when a new Socket must be created for a new connection. To use a derived class, override this method and return an instance of your class. The returned Socket's handle must not be set; Socket has a protected constructor this() to use in this situation.

bind
void bind(Address addr)

Associate a local address with this socket.

close
void close()

Immediately drop any connections and release socket resources. The Socket object is no longer usable after close. Calling shutdown before close is recommended for connection-oriented sockets.

connect
void connect(Address to)

Establish a connection. If the socket is blocking, connect waits for the connection to be made. If the socket is nonblocking, connect returns immediately and the connection attempt is still in progress.

createAddress
Address createAddress()

Can be overridden to support other addresses.

getErrorText
string getErrorText()

Get a text description of this socket's error status, and clear the socket's error status.

getOption
int getOption(SocketOptionLevel level, SocketOption option, void[] result)

Get a socket option.

getOption
int getOption(SocketOptionLevel level, SocketOption option, int32_t result)

Common case of getting integer and boolean options.

getOption
int getOption(SocketOptionLevel level, SocketOption option, Linger result)

Get the linger option.

getOption
void getOption(SocketOptionLevel level, SocketOption option, Duration result)

Get a timeout (duration) option.

listen
void listen(int backlog)

Listen for an incoming connection. bind must be called before you can listen. The backlog is a request of how many pending incoming connections are queued until accepted.

receive
ptrdiff_t receive(void[] buf, SocketFlags flags)
ptrdiff_t receive(void[] buf)

Receive data on the connection. If the socket is blocking, receive waits until there is data to be received.

receiveFrom
ptrdiff_t receiveFrom(void[] buf, SocketFlags flags, Address from)
ptrdiff_t receiveFrom(void[] buf, Address from)
ptrdiff_t receiveFrom(void[] buf, SocketFlags flags)
ptrdiff_t receiveFrom(void[] buf)

Receive data and get the remote endpoint Address. If the socket is blocking, receiveFrom waits until there is data to be received.

send
ptrdiff_t send(const(void)[] buf, SocketFlags flags)
ptrdiff_t send(const(void)[] buf)

Send data on the connection. If the socket is blocking and there is no buffer space left, send waits.

sendTo
ptrdiff_t sendTo(const(void)[] buf, SocketFlags flags, Address to)
ptrdiff_t sendTo(const(void)[] buf, Address to)
ptrdiff_t sendTo(const(void)[] buf, SocketFlags flags)
ptrdiff_t sendTo(const(void)[] buf)

Send data to a specific destination Address. If the destination address is not specified, a connection must have been made and that address is used. If the socket is blocking and there is no buffer space left, sendTo waits.

setKeepAlive
void setKeepAlive(int time, int interval)

Enables TCP keep-alive with the specified parameters.

setOption
void setOption(SocketOptionLevel level, SocketOption option, void[] value)

Set a socket option.

setOption
void setOption(SocketOptionLevel level, SocketOption option, int32_t value)

Common case for setting integer and boolean options.

setOption
void setOption(SocketOptionLevel level, SocketOption option, Linger value)

Set the linger option.

setOption
void setOption(SocketOptionLevel level, SocketOption option, Duration value)

Sets a timeout (duration) option, i.e. SocketOption.SNDTIMEO or RCVTIMEO. Zero indicates no timeout.

shutdown
void shutdown(SocketShutdown how)

Disables sends and/or receives.

Properties

addressFamily
AddressFamily addressFamily [@property getter]

Get the socket's address family.

blocking
bool blocking [@property getter]
bool blocking [@property setter]

Get/set socket's blocking flag.

handle
socket_t handle [@property getter]

Get underlying socket handle.

hostName
string hostName [@property getter]
isAlive
bool isAlive [@property getter]

Property that indicates if this is a valid, alive socket.

localAddress
Address localAddress [@property getter]

Local endpoint Address.

release
socket_t release [@property getter]

Releases the underlying socket handle from the Socket object. Once it is released, you cannot use the Socket object's methods anymore. This also means the Socket destructor will no longer close the socket - it becomes your responsibility.

remoteAddress
Address remoteAddress [@property getter]

Remote endpoint Address.

Static functions

select
int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError)

Wait for a socket to change status. A wait timeout of core.time.Duration or TimeVal, may be specified; if a timeout is not specified or the TimeVal is null, the maximum timeout is used. The TimeVal timeout has an unspecified value when select returns.

select
int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, Duration timeout)
int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, TimeVal* timeout)

Wait for a socket to change status. A wait timeout of core.time.Duration or TimeVal, may be specified; if a timeout is not specified or the TimeVal is null, the maximum timeout is used. The TimeVal timeout has an unspecified value when select returns.

Variables

ERROR
enum int ERROR;

Send or receive error code. See wouldHaveBlocked, lastSocketError and Socket.getErrorText for obtaining more information about the error.

Meta