bandwidth-server-many-up.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <signal.h>
  23. #include <modbus.h>
  24. #if defined(_WIN32)
  25. #include <ws2tcpip.h>
  26. #else
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #endif
  31. #define NB_CONNECTION 5
  32. modbus_t *ctx = NULL;
  33. int server_socket;
  34. modbus_mapping_t *mb_mapping;
  35. static void close_sigint(int dummy)
  36. {
  37. close(server_socket);
  38. modbus_free(ctx);
  39. modbus_mapping_free(mb_mapping);
  40. exit(dummy);
  41. }
  42. int main(void)
  43. {
  44. uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
  45. int master_socket;
  46. int rc;
  47. fd_set refset;
  48. fd_set rdset;
  49. /* Maximum file descriptor number */
  50. int fdmax;
  51. ctx = modbus_new_tcp("127.0.0.1", 1502);
  52. mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,
  53. MODBUS_MAX_READ_REGISTERS, 0);
  54. if (mb_mapping == NULL) {
  55. fprintf(stderr, "Failed to allocate the mapping: %s\n",
  56. modbus_strerror(errno));
  57. modbus_free(ctx);
  58. return -1;
  59. }
  60. server_socket = modbus_tcp_listen(ctx, NB_CONNECTION);
  61. signal(SIGINT, close_sigint);
  62. /* Clear the reference set of socket */
  63. FD_ZERO(&refset);
  64. /* Add the server socket */
  65. FD_SET(server_socket, &refset);
  66. /* Keep track of the max file descriptor */
  67. fdmax = server_socket;
  68. for (;;) {
  69. rdset = refset;
  70. if (select(fdmax+1, &rdset, NULL, NULL, NULL) == -1) {
  71. perror("Server select() failure.");
  72. close_sigint(1);
  73. }
  74. /* Run through the existing connections looking for data to be
  75. * read */
  76. for (master_socket = 0; master_socket <= fdmax; master_socket++) {
  77. if (!FD_ISSET(master_socket, &rdset)) {
  78. continue;
  79. }
  80. if (master_socket == server_socket) {
  81. /* A client is asking a new connection */
  82. socklen_t addrlen;
  83. struct sockaddr_in clientaddr;
  84. int newfd;
  85. /* Handle new connections */
  86. addrlen = sizeof(clientaddr);
  87. memset(&clientaddr, 0, sizeof(clientaddr));
  88. newfd = accept(server_socket, (struct sockaddr *)&clientaddr, &addrlen);
  89. if (newfd == -1) {
  90. perror("Server accept() error");
  91. } else {
  92. FD_SET(newfd, &refset);
  93. if (newfd > fdmax) {
  94. /* Keep track of the maximum */
  95. fdmax = newfd;
  96. }
  97. printf("New connection from %s:%d on socket %d\n",
  98. inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port, newfd);
  99. }
  100. } else {
  101. modbus_set_socket(ctx, master_socket);
  102. rc = modbus_receive(ctx, query);
  103. if (rc > 0) {
  104. modbus_reply(ctx, query, rc, mb_mapping);
  105. } else if (rc == -1) {
  106. /* This example server in ended on connection closing or
  107. * any errors. */
  108. printf("Connection closed on socket %d\n", master_socket);
  109. close(master_socket);
  110. /* Remove from reference set */
  111. FD_CLR(master_socket, &refset);
  112. if (master_socket == fdmax) {
  113. fdmax--;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. return 0;
  120. }