NN_BIND(3)

πŸ“¦
version 1.2.2 Β· 5 versions
, in fedora 44

πŸ’– NN_BIND(3) πŸ’–

okay bestie, buckle up, we're doing nanomsg 1.2.2 today and this one is genuinely a bop. it's nn_bind, it lives in section 3 (the library girlies), and it's about to make your socket ✨social✨.


🌸 NAME 🌸

nn_bind β€” add a local endpoint to the socket 🫢

like. that's it. that's the whole job. you're giving your socket an address so other apps can come find it. it's giving "posting your location in the group chat" energy.


✨ SYNOPSIS ✨

#include <nanomsg/nn.h>

int nn_bind (int s, const char *addr);

two args, one int back. no cap, it's that simple.


πŸ’… DESCRIPTION πŸ’…

So. nn_bind adds a local endpoint to the socket s. Once it's bound, other applications can connect to it. you're basically putting your socket on the map, fr fr.

The addr argument is made of two parts, and the format is:

transport://address
  • the transport part = which underlying transport protocol you're using πŸ”₯
  • the address part = whatever that specific transport protocol decides it means (it's protocol-specific, iykyk)

For the full list of available transport protocols, go check the list on the nanomsg(7) manual page. it's all there, bestie.

The maximum length of the addr parameter is specified by NN_SOCKADDR_MAX, which is defined in the <nanomsg/nn.h> header file. don't go over it, she will not tolerate it.

Also!! important!! nn_bind and nn_connect(3) may be called multiple times on the same socket, which is the whole reason a socket can talk to multiple heterogeneous endpoints at once. it's polyamorous, it's iconic, it's by design. πŸ‘‘


πŸ¦‹ NOTE πŸ¦‹

listen up, this part matters. Unlike with traditional BSD sockets, this function operates asynchronously, and it returns to the caller before the operation is complete. 😭

so yeah β€” as a result, attempts to send data or receive data on the socket may not succeed until the underlying transport actually establishes a connection. it's not broken, it's just still loading. vibe check accordingly. πŸ’€


πŸŽ€ RETURN VALUE πŸŽ€

If the function succeeds, a positive endpoint ID is returned. ✨

That endpoint ID can later be used to remove the endpoint from the socket via the nn_shutdown(3) function. keep it somewhere safe, bestie, it's your receipt.

If the function fails, then -1 is returned and errno is set to one of the values defined below. πŸ™ˆ


πŸ‘‘ ERRORS πŸ‘‘

  • EBADF β€” the provided socket is invalid. girl, that socket is not real.
  • EMFILE β€” maximum number of active endpoints was reached. you've hit the ceiling, no more plus-ones.
  • EINVAL β€” the syntax of the supplied address is invalid. typo era, unfortunately.
  • ENAMETOOLONG β€” the supplied address is too long. she's doing too much.
  • EPROTONOSUPPORT β€” the requested transport protocol is not supported. that transport said "not my department".
  • EADDRNOTAVAIL β€” the requested endpoint is not local. it's giving "that's not yours".
  • ENODEV β€” address specifies a nonexistent interface. that interface ghosted you.
  • EADDRINUSE β€” the requested local endpoint is already in use. occupied!! somebody's already there!!
  • ETERM β€” the library is terminating. it's over, she's leaving, the library is closing. πŸ’€

🌈 EXAMPLE 🌈

s = nn_socket (AF_SP, NN_PUB);
eid1 = nn_bind (s, "inproc://test");
eid2 = nn_bind (s, "tcp://127.0.0.1:5560");

look at her go. two binds, one socket, multiple endpoints. main character energy. πŸ’…


⭐ SEE ALSO ⭐

nn_inproc(7) nn_ipc(7) nn_tcp(7) nn_socket(3) nn_connect(3) nn_shutdown(3) nanomsg(7)


πŸ’œ AUTHORS πŸ’œ

  • Martin Sustrik <sustrik@250bpm.com>
  • Garrett D’Amore <garrett@damore.org>

and that's the whole page, bestie!! huge thanks to the nanomsg girlies β€” this one's from the nanomsg 1.2.2 era, dated 2026-01-16. go bind something cute. mwah. πŸ«ΆπŸ’–βœ¨

πŸ“¦ where this page comes from