bandwidth-server-many-up.c 4.2 KB

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