MODBUS_SET_SOCKET(3)
π MODBUS_SET_SOCKET π
okay bestie, welcome to modbus_set_socket(3) from the libmodbus Manual β yes we're in section 3, we're in the library pages, we're thriving β¨
πΈ NAME πΈ
modbus_set_socket β set socket of the context π§¦
π SYNOPSIS π
int modbus_set_socket(modbus_t *ctx, int s);
β¨ DESCRIPTION β¨
okay so. the modbus_set_socket() function shall set the socket or file descriptor inside the libmodbus context. that's it, that's the whole job, and she does it flawlessly π
the way this is genuinely useful is when you're out here managing multiple client connections to the same server β like, one context, many sockets, rotating through them like it's nothing. main character energy fr fr π
π¦ RETURN VALUE π¦
the function shall return 0 if it's successful. otherwise it shall return -1 and set errno β so like, check your errno, she's trying to tell you something π
π EXAMPLE π
here's the vibe, look at her go:
ctx = modbus_new_tcp("127.0.0.1", 1502);
server_socket = modbus_tcp_listen(ctx, NB_CONNECTION);
FD_ZERO(&rdset);
FD_SET(server_socket, &rdset);
/* .... */
if (FD_ISSET(master_socket, &rdset)) {
modbus_set_socket(ctx, master_socket);
rc = modbus_receive(ctx, query);
if (rc != -1) {
modbus_reply(ctx, query, rc, mb_mapping);
}
}
see how she swaps the socket in with modbus_set_socket(ctx, master_socket) right before receiving? that's the moment. ate, no crumbs π
π SEE ALSO π
modbus_get_socket(3)β the other half of the duo, the getter to this setter, iykyk π
π«Ά AUTHORS π«Ά
the libmodbus documentation was written by StΓ©phane Raimbault <stephane.raimbault@gmail.com> β thank you king, we love the docs πΈ
and that's the whole page bestie!! this was libmodbus v3.1.6, dated 11/17/2025, and she's still iconic. go set that socket, you got this πβ¨ππ¦
π¦ 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