30 #include <sys/socket.h> 31 #include <netinet/in.h> 32 #include <arpa/inet.h> 35 #define NB_CONNECTION 5 64 if (mb_mapping == NULL) {
65 fprintf(stderr,
"Failed to allocate the mapping: %s\n",
85 if (select(fdmax+1, &rdset, NULL, NULL, NULL) == -1) {
86 perror(
"Server select() failure.");
92 for (master_socket = 0; master_socket <= fdmax; master_socket++) {
94 if (FD_ISSET(master_socket, &rdset)) {
98 struct sockaddr_in clientaddr;
102 addrlen =
sizeof(clientaddr);
103 memset(&clientaddr, 0,
sizeof(clientaddr));
104 newfd = accept(
server_socket, (
struct sockaddr *)&clientaddr, &addrlen);
106 perror(
"Server accept() error");
108 FD_SET(newfd, &refset);
114 printf(
"New connection from %s:%d on socket %d\n",
115 inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port, newfd);
127 printf(
"Connection closed on socket %d\n", master_socket);
128 close(master_socket);
131 FD_CLR(master_socket, &refset);
133 if (master_socket == fdmax) {
modbus_mapping_t * modbus_mapping_new(int nb_coil_status, int nb_input_status, int nb_holding_registers, int nb_input_registers)
int modbus_receive(modbus_t *ctx, uint8_t *req)
#define MODBUS_TCP_MAX_ADU_LENGTH
void modbus_free(modbus_t *ctx)
#define MODBUS_MAX_READ_REGISTERS
int modbus_reply(modbus_t *ctx, const uint8_t *req, int req_length, modbus_mapping_t *mb_mapping)
int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
const char * modbus_strerror(int errnum)
void modbus_mapping_free(modbus_mapping_t *mb_mapping)
static void close_sigint(int dummy)
modbus_mapping_t * mb_mapping
void modbus_set_socket(modbus_t *ctx, int socket)
#define MODBUS_MAX_READ_BITS
modbus_t * modbus_new_tcp(const char *ip_address, int port)