This module implements a low-level cross-platform sockets interface. Look at the net
module for the higher-level version.
Port = distinct uint16
Domain = enum AF_UNSPEC = 0, ## unspecified domain (can be detected automatically by ## some procedures, such as getaddrinfo) AF_UNIX = 1, ## for local socket (using a file). Unsupported on Windows. AF_INET = 2, ## for network protocol IPv4 or AF_INET6 = when defined(macosx): 30 else: 23 ## for network protocol IPv6.
SockType = enum SOCK_STREAM = 1, ## reliable stream-oriented service or Stream Sockets SOCK_DGRAM = 2, ## datagram service or Datagram Sockets SOCK_RAW = 3, ## raw protocols atop the network layer. SOCK_SEQPACKET = 5 ## reliable sequenced packet service
Protocol = enum IPPROTO_TCP = 6, ## Transmission control protocol. IPPROTO_UDP = 17, ## User datagram protocol. IPPROTO_IP, ## Internet protocol. Unsupported on Windows. IPPROTO_IPV6, ## Internet Protocol Version 6. Unsupported on Windows. IPPROTO_RAW, ## Raw IP Packets Protocol. Unsupported on Windows. IPPROTO_ICMP ## Control message protocol. Unsupported on Windows.
Servent = object name*: string aliases*: seq[string] port*: Port proto*: string
Hostent = object name*: string aliases*: seq[string] addrtype*: Domain length*: int addrList*: seq[string]
osInvalidSocket = INVALID_SOCKET
IOCPARM_MASK = 127
IOC_IN = -2147483648
FIONBIO = -2147195266
proc ioctlsocket(s: SocketHandle; cmd: clong; argptr: ptr clong): cint {...}{.stdcall, importc: "ioctlsocket", dynlib: "ws2_32.dll".}
proc `==`(a, b: Port): bool {...}{.borrow.}
==
for ports. proc `$`(p: Port): string {...}{.borrow.}
proc toInt(domain: Domain): cint {...}{.raises: [], tags: [].}
cint
. proc toKnownDomain(family: cint): Option[Domain] {...}{.raises: [], tags: [].}
cint
to the Domain or none(), if the cint
is not known. proc toInt(typ: SockType): cint {...}{.raises: [], tags: [].}
cint
. proc toInt(p: Protocol): cint {...}{.raises: [], tags: [].}
cint
. proc toSockType(protocol: Protocol): SockType {...}{.raises: [], tags: [].}
proc createNativeSocket(domain: Domain = AF_INET; sockType: SockType = SOCK_STREAM; protocol: Protocol = IPPROTO_TCP): SocketHandle {...}{.raises: [], tags: [].}
proc createNativeSocket(domain: cint; sockType: cint; protocol: cint): SocketHandle {...}{. raises: [], tags: [].}
Creates a new socket; returns osInvalidSocket if an error occurs.
Use this overload if one of the enums specified above does not contain what you need.
proc newNativeSocket(domain: Domain = AF_INET; sockType: SockType = SOCK_STREAM; protocol: Protocol = IPPROTO_TCP): SocketHandle {...}{.deprecated, raises: [], tags: [].}
Creates a new socket; returns osInvalidSocket if an error occurs.
Deprecated since v0.18.0: Use createNativeSocket
instead.
proc newNativeSocket(domain: cint; sockType: cint; protocol: cint): SocketHandle {...}{. deprecated, raises: [], tags: [].}
Creates a new socket; returns osInvalidSocket if an error occurs.
Use this overload if one of the enums specified above does not contain what you need.
Deprecated since v0.18.0: Use createNativeSocket
instead.
proc close(socket: SocketHandle) {...}{.raises: [], tags: [].}
proc bindAddr(socket: SocketHandle; name: ptr SockAddr; namelen: SockLen): cint {...}{. raises: [], tags: [].}
proc listen(socket: SocketHandle; backlog = SOMAXCONN): cint {...}{.tags: [ReadIOEffect], raises: [].}
socket
as accepting connections. Backlog
specifies the maximum length of the queue of pending connections. proc getAddrInfo(address: string; port: Port; domain: Domain = AF_INET; sockType: SockType = SOCK_STREAM; protocol: Protocol = IPPROTO_TCP): ptr AddrInfo {...}{. raises: [OSError], tags: [].}
Warning: The resulting
ptr AddrInfo
must be freed usingfreeAddrInfo
!
proc dealloc(ai: ptr AddrInfo) {...}{.deprecated, raises: [], tags: [].}
freeAddrInfo
instead. proc ntohl(x: uint32): uint32 {...}{.raises: [], tags: [].}
proc ntohs(x: uint16): uint16 {...}{.raises: [], tags: [].}
proc getServByName(name, proto: string): Servent {...}{.tags: [ReadIOEffect], raises: [OSError].}
Searches the database from the beginning and finds the first entry for which the service name specified by name
matches the s_name member and the protocol name specified by proto
matches the s_proto member.
On posix this will search through the /etc/services
file.
proc getServByPort(port: Port; proto: string): Servent {...}{.tags: [ReadIOEffect], raises: [OSError].}
Searches the database from the beginning and finds the first entry for which the port specified by port
matches the s_port member and the protocol name specified by proto
matches the s_proto member.
On posix this will search through the /etc/services
file.
proc getHostByAddr(ip: string): Hostent {...}{.tags: [ReadIOEffect], raises: [OSError].}
proc getHostByName(name: string): Hostent {...}{.tags: [ReadIOEffect], raises: [OSError].}
proc getHostname(): string {...}{.tags: [ReadIOEffect], raises: [OSError].}
proc getSockDomain(socket: SocketHandle): Domain {...}{.raises: [OSError, IOError], tags: [].}
proc getAddrString(sockAddr: ptr SockAddr): string {...}{. raises: [Exception, OSError, IOError], tags: [RootEffect].}
proc getSockName(socket: SocketHandle): Port {...}{.raises: [OSError], tags: [].}
proc getLocalAddr(socket: SocketHandle; domain: Domain): (string, Port) {...}{. raises: [OSError, Exception], tags: [RootEffect].}
returns the socket's local address and port number.
Similar to POSIX's getsockname.
proc getPeerAddr(socket: SocketHandle; domain: Domain): (string, Port) {...}{. raises: [OSError, Exception], tags: [RootEffect].}
returns the socket's peer address and port number.
Similar to POSIX's getpeername
proc getSockOptInt(socket: SocketHandle; level, optname: int): int {...}{. tags: [ReadIOEffect], raises: [OSError].}
proc setSockOptInt(socket: SocketHandle; level, optname, optval: int) {...}{. tags: [WriteIOEffect], raises: [OSError].}
proc setBlocking(s: SocketHandle; blocking: bool) {...}{.raises: [OSError], tags: [].}
Sets blocking mode on socket.
Raises EOS on error.
proc select(readfds: var seq[SocketHandle]; timeout = 500): int {...}{. deprecated: "use selectRead instead", raises: [], tags: [].}
When a socket in readfds
is ready to be read from then a non-zero value will be returned specifying the count of the sockets which can be read from. The sockets which can be read from will also be removed from readfds
.
timeout
is specified in milliseconds and -1
can be specified for an unlimited time. Warning: This is deprecated since version 0.16.2. Use the selectRead
procedure instead.
proc selectRead(readfds: var seq[SocketHandle]; timeout = 500): int {...}{.raises: [], tags: [].}
When a socket in readfds
is ready to be read from then a non-zero value will be returned specifying the count of the sockets which can be read from. The sockets which can be read from will also be removed from readfds
.
timeout
is specified in milliseconds and -1
can be specified for an unlimited time.
proc selectWrite(writefds: var seq[SocketHandle]; timeout = 500): int {...}{. tags: [ReadIOEffect], raises: [].}
When a socket in writefds
is ready to be written to then a non-zero value will be returned specifying the count of the sockets which can be written to. The sockets which can be written to will also be removed from writefds
.
timeout
is specified in milliseconds and -1
can be specified for an unlimited time.
proc accept(fd: SocketHandle): (SocketHandle, string) {...}{.raises: [], tags: [].}
Accepts a new client connection.
Returns (osInvalidSocket, "") if an error occurred.
template ntohl(x: int32): untyped {...}{.deprecated.}
template ntohs(x: int16): untyped {...}{.deprecated.}
template htonl(x: int32): untyped {...}{.deprecated.}
template htonl(x: uint32): untyped
template htons(x: int16): untyped {...}{.deprecated.}
template htons(x: uint16): untyped
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/nativesockets.html