bandwidth-server-many-up.c 4.2 KB

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