File: modbus_set_socket.md

package info (click to toggle)
libmodbus 3.1.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,112 kB
  • sloc: ansic: 5,495; javascript: 137; makefile: 90; sh: 26
file content (45 lines) | stat: -rw-r--r-- 886 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# modbus_set_socket

## Name

modbus_set_socket - set socket of the context

## Synopsis

```c
int modbus_set_socket(modbus_t *ctx, int s);
```

## Description

The *modbus_set_socket()* function shall set the socket or file descriptor in
the libmodbus context. This function is useful for managing multiple client
connections to the same server.

## Return value

The function shall return 0 if successful. Otherwise it shall return -1 and set errno.

## Example

```c
ctx = modbus_new_tcp("127.0.0.1", 502);
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 also

- [modbus_get_socket](modbus_get_socket.md)