MODBUS_TCP_LISTEN(3)
π 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 connectionbandwidth-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
- debian bookworm: libmodbus-dev 3.1.6-2.1+deb12u1 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms