= nng_listen(3)
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This document is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

== NAME

nng_listen - create and start listener

== SYNOPSIS

[source, c]
----
#include <nng/nng.h>

int nng_listen(nng_socket s, const char *url, nng_listener *lp, int flags);
----

== DESCRIPTION

The `nng_listen()` function creates a newly initialized
xref:nng_listener.5.adoc[`nng_listener`] object, associated with socket _s_,
and configured to listen at the address specified by _url_, and starts it.
If the value of _lp_ is not `NULL`, then
the newly created listener is stored at the address indicated by _lp_.

Listeners are used to accept connections initiated by remote dialers.
An incoming connection generally results in an
xref:nng_pipe.5.adoc[`nng_pipe`] object being created and attached to the socket _s_.
Unlike dialers, listeners generally can create many
pipes, which may be open concurrently.

The _flags_ argument is ignored, but reserved for future use.

TIP: While it is convenient to think of listeners as servers, the
relationship between the listener or dialer is orthogonal to any server or
client status that might be associated with a given protocol.
For example, a xref:nng_req.7.adoc[_req_]
socket might have associated dialers, but might also have associated listeners.
It may even have some of each at the same time!

Because the listener is started immediately, it is generally not possible
to apply extra configuration; if that is needed applications should consider
using xref:nng_listener_create.3.adoc[`nng_listener_create()`] and
xref:nng_listener_start.3.adoc[`nng_listener_start()`] instead.

The created listener will continue to accept new connections, associating
their pipes with the socket, until either it or the socket _s_ is closed.

== RETURN VALUES

This function returns 0 on success, and non-zero otherwise.

== ERRORS

[horizontal]
`NNG_EADDRINUSE`:: The address specified by _url_ is already in use.
`NNG_EADDRINVAL`:: An invalid _url_ was specified.
`NNG_ECLOSED`:: The socket _s_ is not open.
`NNG_EINVAL`:: An invalid set of _flags_ or an invalid _url_ was specified.
`NNG_ENOMEM`:: Insufficient memory is available.

== SEE ALSO

[.text-left]
xref:nng_dial.3.adoc[nng_dial(3)],
xref:nng_listener_close.3.adoc[nng_listener_close(3)],
xref:nng_listener_create.3.adoc[nng_listener_create(3)]
xref:nng_listener_start.3.adoc[nng_listener_start(3)],
xref:nng_strerror.3.adoc[nng_strerror(3)],
xref:nng_listener.5.adoc[nng_listener(5)],
xref:nng.7.adoc[nng(7)]
