Avoid to shadow 'boolean' on weird platform (Windows)
[libmodbus.git] / tests / random-test-server.c
blob772eb0bcd8f3e6ce85527bf131a1d1c3bf843db5
1 /*
2 * Copyright © 2008-2010 Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdio.h>
19 #ifndef _MSC_VER
20 #include <unistd.h>
21 #endif
22 #include <stdlib.h>
23 #include <errno.h>
25 #include <modbus.h>
27 int main(void)
29 int socket = -1;
30 modbus_t *ctx;
31 modbus_mapping_t *mb_mapping;
33 ctx = modbus_new_tcp("127.0.0.1", 1502);
34 /* modbus_set_debug(ctx, TRUE); */
36 mb_mapping = modbus_mapping_new(500, 500, 500, 500);
37 if (mb_mapping == NULL) {
38 fprintf(stderr, "Failed to allocate the mapping: %s\n",
39 modbus_strerror(errno));
40 modbus_free(ctx);
41 return -1;
44 socket = modbus_tcp_listen(ctx, 1);
45 modbus_tcp_accept(ctx, &socket);
47 for (;;) {
48 uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
49 int rc;
51 rc = modbus_receive(ctx, query);
52 if (rc > 0) {
53 /* rc is the query size */
54 modbus_reply(ctx, query, rc, mb_mapping);
55 } else if (rc == -1) {
56 /* Connection closed by the client or error */
57 break;
61 printf("Quit the loop: %s\n", modbus_strerror(errno));
63 if (socket != -1) {
64 close(socket);
66 modbus_mapping_free(mb_mapping);
67 modbus_close(ctx);
68 modbus_free(ctx);
70 return 0;