bandwidth-server-many-up.c
Go to the documentation of this file.
1 /*
2  * Copyright © 2009-2010 Stéphane Raimbault <stephane.raimbault@gmail.com>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <signal.h>
24 
25 #include <modbus.h>
26 
27 #if defined(_WIN32)
28 #include <ws2tcpip.h>
29 #else
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #endif
34 
35 #define NB_CONNECTION 5
36 
37 modbus_t *ctx = NULL;
40 
41 static void close_sigint(int dummy)
42 {
43  close(server_socket);
44  modbus_free(ctx);
45  modbus_mapping_free(mb_mapping);
46 
47  exit(dummy);
48 }
49 
50 int main(void)
51 {
52  int master_socket;
53  int rc;
54  fd_set refset;
55  fd_set rdset;
56 
57  /* Maximum file descriptor number */
58  int fdmax;
59 
60  ctx = modbus_new_tcp("127.0.0.1", 1502);
61 
64  if (mb_mapping == NULL) {
65  fprintf(stderr, "Failed to allocate the mapping: %s\n",
66  modbus_strerror(errno));
67  modbus_free(ctx);
68  return -1;
69  }
70 
72 
73  signal(SIGINT, close_sigint);
74 
75  /* Clear the reference set of socket */
76  FD_ZERO(&refset);
77  /* Add the server socket */
78  FD_SET(server_socket, &refset);
79 
80  /* Keep track of the max file descriptor */
81  fdmax = server_socket;
82 
83  for (;;) {
84  rdset = refset;
85  if (select(fdmax+1, &rdset, NULL, NULL, NULL) == -1) {
86  perror("Server select() failure.");
87  close_sigint(1);
88  }
89 
90  /* Run through the existing connections looking for data to be
91  * read */
92  for (master_socket = 0; master_socket <= fdmax; master_socket++) {
93 
94  if (FD_ISSET(master_socket, &rdset)) {
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;
100 
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);
109 
110  if (newfd > fdmax) {
111  /* Keep track of the maximum */
112  fdmax = newfd;
113  }
114  printf("New connection from %s:%d on socket %d\n",
115  inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port, newfd);
116  }
117  } else {
118  /* An already connected master has sent a new query */
119  uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
120 
121  modbus_set_socket(ctx, master_socket);
122  rc = modbus_receive(ctx, query);
123  if (rc != -1) {
124  modbus_reply(ctx, query, rc, mb_mapping);
125  } else {
126  /* Connection closed by the client, end of server */
127  printf("Connection closed on socket %d\n", master_socket);
128  close(master_socket);
129 
130  /* Remove from reference set */
131  FD_CLR(master_socket, &refset);
132 
133  if (master_socket == fdmax) {
134  fdmax--;
135  }
136  }
137  }
138  }
139  }
140  }
141 
142  return 0;
143 }
modbus_mapping_t * modbus_mapping_new(int nb_coil_status, int nb_input_status, int nb_holding_registers, int nb_input_registers)
Definition: modbus.c:1547
int modbus_receive(modbus_t *ctx, uint8_t *req)
Definition: modbus.c:460
#define MODBUS_TCP_MAX_ADU_LENGTH
Definition: modbus-tcp.h:41
void modbus_free(modbus_t *ctx)
Definition: modbus.c:1528
#define MODBUS_MAX_READ_REGISTERS
Definition: modbus.h:80
int modbus_reply(modbus_t *ctx, const uint8_t *req, int req_length, modbus_mapping_t *mb_mapping)
Definition: modbus.c:641
int server_socket
int main(void)
int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
Definition: modbus-tcp.c:387
#define NB_CONNECTION
modbus_t * ctx
const char * modbus_strerror(int errnum)
Definition: modbus.c:53
void modbus_mapping_free(modbus_mapping_t *mb_mapping)
Definition: modbus.c:1625
static void close_sigint(int dummy)
modbus_mapping_t * mb_mapping
void modbus_set_socket(modbus_t *ctx, int socket)
Definition: modbus.c:1478
#define MODBUS_MAX_READ_BITS
Definition: modbus.h:70
modbus_t * modbus_new_tcp(const char *ip_address, int port)
Definition: modbus-tcp.c:636


libmodbus
Author(s):
autogenerated on Sat Nov 21 2020 03:17:32