Janet 1.32.1-cc5beda Documentation
(Other Versions: 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 cfunction source
(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

net/accept-loop cfunction source
(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

net/address cfunction source
(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

net/address-unpack cfunction source
(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

net/chunk cfunction source
(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

net/close function source
(net/close stream)

Alias for `ev/close`.
Community Examples

net/connect cfunction source
(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

net/flush cfunction source
(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

net/listen cfunction source
(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

net/localname cfunction source
(net/localname stream)

Gets the local address and port in a tuple in that order.
Community Examples

net/peername cfunction source
(net/peername stream)

Gets the remote peer's address and port in a tuple in that order.
Community Examples

net/read cfunction source
(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

net/recv-from cfunction source
(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

net/send-to cfunction source
(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

net/server function source
(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

net/setsockopt cfunction source
(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
Community Examples

net/shutdown cfunction source
(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

net/write cfunction source
(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