This module implements a low-level cross-platform sockets interface. Look at the net module for the higher-level version.
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 = 23Hostent = object name*: string aliases*: seq[string] addrtype*: Domain length*: int addrList*: seq[string]
Protocol = enum IPPROTO_TCP = 6, ## Transmission control protocol. IPPROTO_UDP = 17, ## User datagram protocol. IPPROTO_IP, ## Internet protocol. IPPROTO_IPV6, ## Internet Protocol Version 6. IPPROTO_RAW, ## Raw IP Packets Protocol. Unsupported on Windows. IPPROTO_ICMP, ## Internet Control message protocol. IPPROTO_ICMPV6 ## Internet Control message protocol for IPv6.
socket proc Source Edit proc accept(fd: SocketHandle; inheritable = defined(nimInheritHandles)): (
SocketHandle, string) {....raises: [], tags: [], forbids: [].}Accepts a new client connection.
inheritable decides if the resulting SocketHandle can be inherited by child processes.
Returns (osInvalidSocket, "") if an error occurred.
Source Editproc createNativeSocket(domain: cint; sockType: cint; protocol: cint;
inheritable: bool = defined(nimInheritHandles)): SocketHandle {.
...raises: [], tags: [], forbids: [].}Creates a new socket; returns osInvalidSocket if an error occurs.
inheritable decides if the resulting SocketHandle can be inherited by child processes.
Use this overload if one of the enums specified above does not contain what you need.
Source Editproc createNativeSocket(domain: Domain = AF_INET;
sockType: SockType = SOCK_STREAM;
protocol: Protocol = IPPROTO_TCP;
inheritable: bool = defined(nimInheritHandles)): SocketHandle {.
...raises: [], tags: [], forbids: [].}Creates a new socket; returns osInvalidSocket if an error occurs.
inheritable decides if the resulting SocketHandle can be inherited by child processes.
proc getServByName(name, proto: string): Servent {....tags: [ReadIOEffect],
raises: [OSError], forbids: [].}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], forbids: [].}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 selectRead(readfds: var seq[SocketHandle]; timeout = 500): int {.
...raises: [], tags: [], forbids: [].}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 cannot 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: [], forbids: [].}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 cannot be written to will also be removed from writefds.
timeout is specified in milliseconds and -1 can be specified for an unlimited time.
proc setInheritable(s: SocketHandle; inheritable: bool): bool {.inline,
...raises: [], tags: [], forbids: [].}Set whether a socket is inheritable by child processes. Returns true on success.
This function is not implemented on all platform, test for availability with declared() <system.html#declared,untyped>.
proc toInt(domain: Domain): cint {....raises: [], tags: [], forbids: [].}cint. Source Edit
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/nativesockets.html