std.socket
Socket primitives. Example: See listener.d and htmlget.d
License
Module Initializers 1
()Module Deinitializers 1
()Types 30
Base exception thrown by std.socket.
Socket exception representing network errors reported by the operating system.
int errorCodethis(string msg,
string file = __FILE__,
size_t line = __LINE__,
Throwable next = null,
int err = _lasterr(),
string function(int) @trusted errorFormatter = & formatSocketError)this(string msg,
Throwable next,
string file = __FILE__,
size_t line = __LINE__,
int err = _lasterr(),
string function(int) @trusted errorFormatter = & formatSocketError)this(string msg,
int err,
string function(int) @trusted errorFormatter = & formatSocketError,
string file = __FILE__,
size_t line = __LINE__,
Throwable next = null)Socket exception representing invalid parameters specified by user code.
Socket exception representing attempts to use network capabilities not available on the current system.
The communication domain used to resolve an address.
Communication semantics
Protocol
Class for retrieving protocol information.
Example:
auto proto = new Protocol;
writeln("About protocol TCP:");
if (proto.getProtocolByType(ProtocolType.TCP))
{
writefln(" Name: %s", proto.name);
foreach (string s; proto.aliases)
writefln(" Alias: %s", s);
}
else
writeln(" No information found");ProtocolType typeThese members are populated when one of the following functions are called successfully:string namestring[] aliasesClass for retrieving service information.
Example:
auto serv = new Service;
writeln("About service epmap:");
if (serv.getServiceByName("epmap", "tcp"))
{
writefln(" Service: %s", serv.name);
writefln(" Port: %d", serv.port);
writefln(" Protocol: %s", serv.protocolName);
foreach (string s; serv.aliases)
writefln(" Alias: %s", s);
}
else
writefln(" No service for epmap.");string nameThese members are populated when one of the following functions are called successfully:string[] aliasesushort portstring protocolNamebool getServiceByName(scope const(char)[] name, scope const(char)[] protocolName = null) @trusted nothrowIf a protocol name is omitted, any protocol will be matched. Returns: false on failure.Class for exceptions thrown from an InternetHost.
Class for resolving IPv4 addresses.
Consider using getAddress, parseAddress and Address methods instead of using this class directly.
string nameThese members are populated when one of the following functions are called successfully:string[] aliasesuint[] addrListvoid validHostent(in hostent * he)bool getHostByName(scope const(char)[] name) @trustedResolve host name. Returns: false if unable to resolve.bool getHostByAddr(scope const(char)[] addr) @trustedSame as previous, but addr is an IPv4 address string in the dotted-decimal form a.b.c.d. Returns: false if unable to resolve.Holds information about a socket _address retrieved by getAddressInfo.
A subset of flags supported on all platforms with getaddrinfo. Specifies option flags for getAddressInfo.
Class for exceptions thrown from an Address.
Abstract class for representing a socket address.
Example:
writeln("About www.google.com port 80:");
try
{
Address[] addresses = getAddress("www.google.com", 80);
writefln(" %d addresses found.", addresses.length);
foreach (int i, Address a; addresses)
{
writefln(" Address %d:", i+1);
writefln(" IP address: %s", a.toAddrString());
writefln(" Hostname: %s", a.toHostNameString());
writefln(" Port: %s", a.toPortString());
writefln(" Service name: %s",
a.toServiceNameString());
}
}
catch (SocketException e)
writefln(" Lookup error: %s", e.msg);socklen_t nameLen() @property const pure nothrow @nogcReturns actual size of underlying `sockaddr` structure.void setNameLen(socklen_t len)Encapsulates a reference to an arbitrary socket address.
sockaddr * sasocklen_t lensockaddr * name() @propertyEncapsulates an IPv4 (Internet Protocol version 4) socket address.
Consider using getAddress, parseAddress and Address methods instead of using this class directly.
sockaddr_in sinuint ADDR_ANYuint ADDR_NONEushort PORT_ANYstring toAddrString() @trusted constHuman readable string representing the IPv4 address in dotted-decimal form.bool opEquals(Object o) constProvides support for comparing equality with another InternetAddress of the same type. Returns: true if the InternetAddresses share the same address and port number.uint parse(scope const(char)[] addr) @trusted nothrowParse an IPv4 address string in the dotted-decimal form a.b.c.d and return the number. Returns: If the string is not a legitimate IPv4 address, `ADDR_NONE` is returned.string addrToString(uint addr) @trusted nothrowConvert an IPv4 address number in host byte order to a human readable string representing the IPv4 address in dotted-decimal form.this()this(scope const(char)[] addr, ushort port)Construct a new `InternetAddress`. Params: addr = an IPv4 address string in the dotted-decimal form a.b.c.d, or a host name which will be resolved using an `InternetHost` object. port = port number...this(uint addr, ushort port)Construct a new `InternetAddress`. Params: addr = (optional) an IPv4 address in host byte order, may be `ADDRANY`. port = port number, may be `PORTANY`.this(ushort port)dittothis(sockaddr_in addr)Construct a new `InternetAddress`. Params: addr = A sockaddr_in as obtained from lower-level API calls such as getifaddrs.Encapsulates an IPv6 (Internet Protocol version 6) socket address.
Consider using getAddress, parseAddress and Address methods instead of using this class directly.
sockaddr_in6 sin6ushort PORT_ANYAny IPv6 port number.ubyte[16] parse(scope const(char)[] addr) @trustedParse an IPv6 host address string as described in RFC 2373, and return the address. Throws: `SocketException` on error.this()this(scope const(char)[] addr, scope const(char)[] service = null)Construct a new `Internet6Address`. Params: addr = an IPv6 host address string in the form described in RFC 2373, or a host name which will be resolved using `getAddressInfo`. service = (optiona...this(scope const(char)[] addr, ushort port)Construct a new `Internet6Address`. Params: addr = an IPv6 host address string in the form described in RFC 2373, or a host name which will be resolved using `getAddressInfo`. port = port number, m...this(ubyte[16] addr, ushort port)Construct a new `Internet6Address`. Params: addr = (optional) an IPv6 host address in host byte order, or `ADDRANY`. port = port number, may be `PORTANY`.this(ushort port)dittothis(sockaddr_in6 addr)Construct a new `Internet6Address`. Params: addr = A sockaddr_in6 as obtained from lower-level API calls such as getifaddrs.Exception thrown by Socket.accept.
How a socket is shutdown:
Socket flags that may be OR'ed together:
Duration timeout value.
_ctimeval ctimevalinout(tv_usec_t) microseconds() pure nothrow @nogc @property ref inout returnNumber of additional microseconds.A collection of sockets for use with Socket.select.
SocketSet wraps the platform fd_set type. However, unlike fd_set, SocketSet is not statically limited to FD_SETSIZE or any other limit, and grows as needed.
void reset() pure nothrow @nogcReset the `SocketSet` so that there are 0 `Socket`s in the collection.void add(Socket s) pure nothrowAdd a `Socket` to the collection. The socket must not already be in the collection.void remove(Socket s) pure nothrowRemove this `Socket` from the collection. Does nothing if the socket is not in the collection already.uint max() @property const pure nothrow @nogcReturns: The current capacity of this `SocketSet`. The exact meaning of the return value varies from platform to platform.this(size_t size = FD_SETSIZE)Create a SocketSet with a specific initial capacity (defaults to `FD_SETSIZE`, the system's default capacity).The level at which a socket option is defined:
Specifies a socket option:
Class that creates a network communication endpoint using the Berkeley sockets interface.
socket_t sockAddressFamily _family500 WINSOCK_TIMEOUT_SKEWint ERRORSend or receive error code. See `wouldHaveBlocked`, `lastSocketError` and `Socket.getErrorText` for obtaining more information about the error.void setSock(socket_t handle)socket_t release() @property pure nothrow @nogcReleases 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...void connect(Address to) @trustedEstablish 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 prog...void listen(int backlog) @trustedListen 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 `accept`ed.Socket accepting() pure nothrowCalled 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 n...Socket accept() @trustedAccept 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.void close() scope @trusted nothrow @nogcImmediately 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.ptrdiff_t send(scope const(void)[] buf, SocketFlags flags) @trustedSend data on the connection. If the socket is blocking and there is no buffer space left, `send` waits. Returns: The number of bytes actually sent, or `Socket.ERROR` on failure.ptrdiff_t send(scope const(void)[] buf)dittoptrdiff_t sendTo(scope const(void)[] buf, SocketFlags flags, Address to) @trustedSend 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...ptrdiff_t sendTo(scope const(void)[] buf)dittoptrdiff_t receive(scope void[] buf, SocketFlags flags) @trustedReceive data on the connection. If the socket is blocking, `receive` waits until there is data to be received. Returns: The number of bytes actually received, `0` if the remote side has closed the ...ptrdiff_t receive(scope void[] buf)dittoptrdiff_t receiveFrom(scope void[] buf, SocketFlags flags, ref Address from) @trustedReceive data and get the remote endpoint `Address`. If the socket is blocking, `receiveFrom` waits until there is data to be received. Returns: The number of bytes actually received, `0` if the rem...ptrdiff_t receiveFrom(scope void[] buf, ref Address from)dittoptrdiff_t receiveFrom(scope void[] buf)dittoint getOption(SocketOptionLevel level, SocketOption option, scope void[] result) @trustedGet a socket option. Returns: The number of bytes written to `result`. The length, in bytes, of the actual result - very different from getsockopt()int getOption(SocketOptionLevel level, SocketOption option, out int32_t result) @trustedCommon case of getting integer and boolean options.int getOption(SocketOptionLevel level, SocketOption option, out Linger result) @trustedGet the linger option.void getOption(SocketOptionLevel level, SocketOption option, out Duration result) @trustedGet a timeout (duration) option.void setOption(SocketOptionLevel level, SocketOption option, scope void[] value) @trustedSet a socket option.void setOption(SocketOptionLevel level, SocketOption option, int32_t value) @trustedCommon case for setting integer and boolean options.void setOption(SocketOptionLevel level, SocketOption option, Linger value) @trustedSet the linger option.void setOption(SocketOptionLevel level, SocketOption option, Duration value) @trustedSets a timeout (duration) option, i.e. `SocketOption.SNDTIMEO` or `RCVTIMEO`. Zero indicates no timeout.string getErrorText()Get a text description of this socket's error status, and clear the socket's error status.void setKeepAlive(int time, int interval) @trustedEnables TCP keep-alive with the specified parameters.int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, Duration timeout) @trustedWait for a socket to change status. A wait timeout of Duration or `TimeVal`, may be specified; if a timeout is not specified or the `TimeVal` is `null`, the maximum timeout is used. The `TimeVal` t...int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, TimeVal * timeout) @trustedDittoAddress createAddress() pure nothrowCan be overridden to support other addresses. Returns: A new `Address` object for the current address family.this()this(AddressFamily af, SocketType type, ProtocolType protocol)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(AddressFamily af, SocketType type)dittothis(AddressFamily af, SocketType type, scope const(char)[] protocolName)dittothis(const scope AddressInfo info)Create a blocking socket using the parameters from the specified `AddressInfo` structure.this(socket_t sock, AddressFamily af)Use an existing socket handle.Shortcut class for a TCP Socket.
this(AddressFamily family)Constructs a blocking TCP Socket.this()Constructs a blocking IPv4 TCP Socket.Shortcut class for a UDP Socket.
this(AddressFamily family)Constructs a blocking UDP Socket.this()Constructs a blocking IPv4 UDP Socket.Functions 12
string lastSocketError() @propertyReturns the error message of the most recently encountered network error.bool wouldHaveBlocked() nothrow @nogcReturns: `true` if the last socket operation failed because the socket was in non-blocking mode and the operation would have blocked, or if the socket is in blocking mode and set a `SNDTIMEO` or `R...string formatGaiError(int err) @trustedOn POSIX, getaddrinfo uses its own error codes, and thus has its own formatting function.AddressInfo[] getAddressInfo(T...)(scope const(char)[] node, scope T options)Provides protocol-independent translation from host names to socket addresses. If advanced functionality is not required, consider using `getAddress` for compatibility with older systems.AddressInfo[] getAddressInfoImpl(scope const(char)[] node, scope const(char)[] service, addrinfo * hints) @systemAddress[] getAddress(scope const(char)[] hostname, scope const(char)[] service = null)Provides protocol-independent translation from host names to socket addresses. Uses `getAddressInfo` if the current system supports it, and `InternetHost` otherwise.Address parseAddress(scope const(char)[] hostaddr, scope const(char)[] service = null)Provides protocol-independent parsing of network addresses. Does not attempt name resolution. Uses `getAddressInfo` with `AddressInfoFlags.NUMERICHOST` if the current system supports it, and `Inter...Variables 3
typeof(& getnameinfo) getnameinfoPointertypeof(& getaddrinfo) getaddrinfoPointertypeof(& freeaddrinfo) freeaddrinfoPointer