Janet 1.27.0-01aab66 Documentation
(Other Versions: 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/shutdown net/write

cfunction (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 return nil. Returns a new duplex stream which represents a connection to the client.
Community Examples / source
cfunction (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.
Community Examples / source
cfunction (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.
Community Examples / source
cfunction (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.
Community Examples / source
cfunction (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 return nil.
Community Examples / source
function (net/close stream)
Alias for `ev/close`.
Community Examples / source
cfunction (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. 
Community Examples / source
cfunction (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.
Community Examples / source
cfunction (net/listen host port &opt type)
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.
Community Examples / source
cfunction (net/localname stream)
Gets the local address and port in a tuple in that order.
Community Examples / source
cfunction (net/peername stream)
Gets the remote peer's address and port in a tuple in that order.
Community Examples / source
cfunction (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 return nil. Returns a buffer with up to n more bytes in it, or raises an error if the read failed.
Community Examples / source
cfunction (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 return nil.
Community Examples / source
cfunction (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 return nil. Returns stream.
Community Examples / source
function (net/server host port &opt handler type)
Start a server asynchronously with `net/listen` and `net/accept-loop`. Returns the new server stream.
Community Examples / source
cfunction (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.
Community Examples / source
cfunction (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 return nil. Returns nil, or raises an error if the write failed.
Community Examples / source