MODBUS_TCP_LISTEN(3)

πŸ“¦
version 3.1.6 Β· 3 versions
, in debian bookworm

πŸ’– MODBUS_TCP_LISTEN(3) πŸ’–

okay bestie, welcome to the libmodbus Manual page for MODBUS_TCP_LISTEN β€” she's the IPv4-flavored queen of opening a TCP Modbus socket and just waiting for her little connection babies to arrive. ✨


🌸 NAME 🌸

modbus_tcp_listen β€” create and listen a TCP Modbus socket (IPv4)


πŸŽ€ SYNOPSIS πŸŽ€

int modbus_tcp_listen(modbus_t *ctx, int nb_connection);

πŸ’… DESCRIPTION πŸ’…

So here's the tea: modbus_tcp_listen() is gonna create a socket and listen to a maximum of nb_connection incoming connections on the IP address you specified. Iconic behaviour, no cap. 🫢

The context ctx must be allocated and initialized with modbus_new_tcp(3) before you set the IP address to listen. That's the prerequisite, bestie, do not skip it. 😭

And here's the fun part: if the IP address is set to NULL or 0.0.0.0, then any addresses will be listen. She's like "come one, come all" β€” full main character energy. πŸ‘‘


⭐ RETURN VALUE ⭐

The function shall return a new socket if successful. Slay. πŸ’–

Otherwise it shall return -1 and set errno. Yeah, that's the sad path. πŸ’€


🌈 EXAMPLE 🌈

For detailed examples, see source files in the tests directory:

  • unit-test-server.c, simple but handle only one connection
  • bandwidth-server-many-up.c, handles several connections at once
...

/* To listen any addresses on port 502 */
ctx = modbus_new_tcp(NULL, 502);

/* Handle until 10 established connections */
server_socket = modbus_tcp_listen(ctx, 10);

/* Clear the reference set of socket */
FD_ZERO(&refset);

/* Add the server socket */
FD_SET(server_socket, &refset);

if (select(server_socket + 1, &refset, NULL, NULL, NULL) == -1) {
}

...

close(server_socket);
modbus_free(ctx);

πŸ¦‹ SEE ALSO πŸ¦‹

modbus_new_tcp(3) modbus_tcp_accept(3) modbus_tcp_pi_listen(3)


πŸ‘‘ AUTHORS πŸ‘‘

The libmodbus documentation was written by StΓ©phane Raimbault <stephane.raimbault@gmail.com>


and that's a wrap, bestie πŸŽ€ this page came from libmodbus v3.1.6, dated 11/17/2025 β€” go forth and listen to those connections, periodt. πŸ’œβœ¨

πŸ“¦ where this page comes from