MODBUS_SET_SOCKET(3)

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

πŸ’– 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 πŸ‘€


🫢 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