Janet 1.38.0-73334f3 Documentation
(Other Versions:
1.37.1
1.36.0
1.35.0
1.34.0
1.31.0
1.29.1
1.28.0
1.27.0
1.26.0
1.25.1
1.24.0
1.23.0
1.22.0
1.21.0
1.20.0
1.19.0
1.18.1
1.17.1
1.16.1
1.15.0
1.13.1
1.12.2
1.11.1
1.10.1
1.9.1
1.8.1
1.7.0
1.6.0
1.5.1
1.5.0
1.4.0
1.3.1
)
Net Module
Index
net/accept net/accept-loop net/address net/address-unpack net/chunk net/close net/connect net/flush net/listen net/localname net/peername net/read net/recv-from net/send-to net/server net/setsockopt net/shutdown net/write
(net/accept stream &opt timeout)
Get the next connection on a server stream. This would usually be called in a loop in a dedicated fiber. Takes an optional timeout in seconds, after which will raise an error. Returns a new duplex stream which represents a connection to the client.
(net/accept-loop stream handler)
Shorthand for running a server stream that will continuously accept new connections. Blocks the current fiber until the stream is closed, and will return the stream.
(net/address host port &opt type multi)
Look up the connection information for a given hostname, port, and connection type. Returns a handle that can
be used to send datagrams over network without establishing a connection. On Posix platforms, you can use :unix
for host to connect to a unix domain socket, where the name is given in the port argument. On Linux, abstract
unix domain sockets are specified with a leading '@' character in port. If multi
is truthy, will return all
address that match in an array instead of just the first.
(net/address-unpack address)
Given an address returned by net/address, return a host, port pair. Unix domain sockets will have only the path in the returned tuple.
(net/chunk stream nbytes &opt buf timeout)
Same a net/read, but will wait for all n bytes to arrive rather than return early. Takes an optional timeout in seconds, after which will raise an error.
(net/connect host port &opt type bindhost bindport)
Open a connection to communicate with a server. Returns a duplex stream that can be used to communicate with the server. Type is an optional keyword to specify a connection type, either :stream or :datagram. The default is :stream. Bindhost is an optional string to select from what address to make the outgoing connection, with the default being the same as using the OS's preferred address.
(net/flush stream)
Make sure that a stream is not buffering any data. This temporarily disables Nagle's algorithm. Use this to make sure data is sent without delay. Returns stream.
(net/listen host port &opt type no-reuse)
Creates a server. Returns a new stream that is neither readable nor writeable. Use net/accept or
net/accept-loop be to handle connections and start the server. The type parameter specifies the type of network
connection, either a :stream (usually tcp), or :datagram (usually udp). If not specified, the default is
:stream. The host and port arguments are the same as in net/address. The last boolean parameter no-reuse
will
disable the use of SO_REUSEADDR and SO_REUSEPORT when creating a server on some operating systems.
(net/localname stream)
Gets the local address and port in a tuple in that order.
(net/peername stream)
Gets the remote peer's address and port in a tuple in that order.
(net/read stream nbytes &opt buf timeout)
Read up to n bytes from a stream, suspending the current fiber until the bytes are available. n
can also be
the keyword :all
to read into the buffer until end of stream. If less than n bytes are available (and more
than 0), will push those bytes and return early. Takes an optional timeout in seconds, after which will raise
an error. Returns a buffer with up to n more bytes in it, or raises an error if the read failed.
(net/recv-from stream nbytes buf &opt timeout)
Receives data from a server stream and puts it into a buffer. Returns the socket-address the packet came from. Takes an optional timeout in seconds, after which will raise an error.
(net/send-to stream dest data &opt timeout)
Writes a datagram to a server stream. dest is a the destination address of the packet. Takes an optional timeout in seconds, after which will raise an error. Returns stream.
(net/server host port &opt handler type no-reuse)
Start a server asynchronously with net/listen
and net/accept-loop
. Returns the new server stream.
(net/setsockopt stream option value)
set socket options.
supported options and associated value types:
- :so-broadcast boolean
- :so-reuseaddr boolean
- :so-keepalive boolean
- :ip-multicast-ttl number
- :ip-add-membership string
- :ip-drop-membership string
- :ipv6-join-group string
- :ipv6-leave-group string
(net/shutdown stream &opt mode)
Stop communication on this socket in a graceful manner, either in both directions or just reading/writing from
the stream. The mode
parameter controls which communication to stop on the socket.
-
:wr
is the default and prevents both reading new data from the socket and writing new data to the socket. -
:r
disables reading new data from the socket. -
:w
disable writing data to the socket.
Returns the original socket.
(net/write stream data &opt timeout)
Write data to a stream, suspending the current fiber until the write completes. Takes an optional timeout in seconds, after which will raise an error. Returns nil, or raises an error if the write failed.