Link against socket and nsl libs when building on SunOS
[libmodbus.git] / tests / bandwidth-server-many-up.c
blobf6eb7d7fbe92b54d22bb3b1214eee01db1162fa6
1 /*
2 * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
7 #include <errno.h>
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
14 #include <modbus.h>
16 #if defined(_WIN32)
17 #include <ws2tcpip.h>
18 #else
19 #include <arpa/inet.h>
20 #include <netinet/in.h>
21 #include <sys/select.h>
22 #include <sys/socket.h>
23 #endif
25 #define NB_CONNECTION 5
27 static modbus_t *ctx = NULL;
28 static modbus_mapping_t *mb_mapping;
30 static int server_socket = -1;
32 static void close_sigint(int dummy)
34 if (server_socket != -1) {
35 close(server_socket);
37 modbus_free(ctx);
38 modbus_mapping_free(mb_mapping);
40 exit(dummy);
43 int main(void)
45 uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
46 int master_socket;
47 int rc;
48 fd_set refset;
49 fd_set rdset;
50 /* Maximum file descriptor number */
51 int fdmax;
53 ctx = modbus_new_tcp("127.0.0.1", 1502);
55 mb_mapping =
56 modbus_mapping_new(MODBUS_MAX_READ_BITS, 0, MODBUS_MAX_READ_REGISTERS, 0);
57 if (mb_mapping == NULL) {
58 fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
59 modbus_free(ctx);
60 return -1;
63 server_socket = modbus_tcp_listen(ctx, NB_CONNECTION);
64 if (server_socket == -1) {
65 fprintf(stderr, "Unable to listen TCP connection\n");
66 modbus_free(ctx);
67 return -1;
70 signal(SIGINT, close_sigint);
72 /* Clear the reference set of socket */
73 FD_ZERO(&refset);
74 /* Add the server socket */
75 FD_SET(server_socket, &refset);
77 /* Keep track of the max file descriptor */
78 fdmax = server_socket;
80 for (;;) {
81 rdset = refset;
82 if (select(fdmax + 1, &rdset, NULL, NULL, NULL) == -1) {
83 perror("Server select() failure.");
84 close_sigint(1);
87 /* Run through the existing connections looking for data to be
88 * read */
89 for (master_socket = 0; master_socket <= fdmax; master_socket++) {
91 if (!FD_ISSET(master_socket, &rdset)) {
92 continue;
95 if (master_socket == server_socket) {
96 /* A client is asking a new connection */
97 socklen_t addrlen;
98 struct sockaddr_in clientaddr;
99 int newfd;
101 /* Handle new connections */
102 addrlen = sizeof(clientaddr);
103 memset(&clientaddr, 0, sizeof(clientaddr));
104 newfd = accept(server_socket, (struct sockaddr *) &clientaddr, &addrlen);
105 if (newfd == -1) {
106 perror("Server accept() error");
107 } else {
108 FD_SET(newfd, &refset);
110 if (newfd > fdmax) {
111 /* Keep track of the maximum */
112 fdmax = newfd;
114 printf("New connection from %s:%d on socket %d\n",
115 inet_ntoa(clientaddr.sin_addr),
116 clientaddr.sin_port,
117 newfd);
119 } else {
120 modbus_set_socket(ctx, master_socket);
121 rc = modbus_receive(ctx, query);
122 if (rc > 0) {
123 modbus_reply(ctx, query, rc, mb_mapping);
124 } else if (rc == -1) {
125 /* This example server in ended on connection closing or
126 * any errors. */
127 printf("Connection closed on socket %d\n", master_socket);
128 close(master_socket);
130 /* Remove from reference set */
131 FD_CLR(master_socket, &refset);
133 if (master_socket == fdmax) {
134 fdmax--;
141 return 0;