1MODBUS_TCP_LISTEN(3) libmodbus Manual MODBUS_TCP_LISTEN(3)
2
3
4
6 modbus_tcp_listen - create and listen a TCP Modbus socket (IPv4)
7
9 int modbus_tcp_listen(modbus_t *ctx, int nb_connection);
10
12 The modbus_tcp_listen() function shall create a socket and listen to
13 maximum nb_connection incoming connections on the specified IP address.
14 The context _ctx _must be allocated and initialized with
15 modbus_new_tcp(3) before to set the IP address to listen, if IP address
16 is set to NULL or 0.0.0.0, any addresses will be listen.
17
19 The function shall return a new socket if successful. Otherwise it
20 shall return -1 and set errno.
21
23 For detailed examples, see source files in tests directory:
24
25 • unit-test-server.c, simple but handle only one connection
26
27 • bandwidth-server-many-up.c, handles several connections at once
28
29 ...
30
31 /* To listen any addresses on port 502 */
32 ctx = modbus_new_tcp(NULL, 502);
33
34 /* Handle until 10 established connections */
35 server_socket = modbus_tcp_listen(ctx, 10);
36
37 /* Clear the reference set of socket */
38 FD_ZERO(&refset);
39
40 /* Add the server socket */
41 FD_SET(server_socket, &refset);
42
43 if (select(server_socket + 1, &refset, NULL, NULL, NULL) == -1) {
44 }
45
46 ...
47
48 close(server_socket);
49 modbus_free(ctx);
50
52 modbus_new_tcp(3) modbus_tcp_accept(3) modbus_tcp_pi_listen(3)
53
55 The libmodbus documentation was written by Stéphane Raimbault
56 <stephane.raimbault@gmail.com>
57
58
59
60libmodbus v3.1.6 07/21/2022 MODBUS_TCP_LISTEN(3)