inetsrv.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Byte Stream Connection Server Example
  2. Copyright (C) 1991-2019 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include <netinet/in.h>
  21. #include <netdb.h>
  22. #define PORT 5555
  23. #define MAXMSG 512
  24. int
  25. read_from_client (int filedes)
  26. {
  27. char buffer[MAXMSG];
  28. int nbytes;
  29. nbytes = read (filedes, buffer, MAXMSG);
  30. if (nbytes < 0)
  31. {
  32. /* Read error. */
  33. perror ("read");
  34. exit (EXIT_FAILURE);
  35. }
  36. else if (nbytes == 0)
  37. /* End-of-file. */
  38. return -1;
  39. else
  40. {
  41. /* Data read. */
  42. fprintf (stderr, "Server: got message: `%s'\n", buffer);
  43. return 0;
  44. }
  45. }
  46. int
  47. main (void)
  48. {
  49. extern int make_socket (uint16_t port);
  50. int sock;
  51. fd_set active_fd_set, read_fd_set;
  52. int i;
  53. struct sockaddr_in clientname;
  54. size_t size;
  55. /* Create the socket and set it up to accept connections. */
  56. sock = make_socket (PORT);
  57. if (listen (sock, 1) < 0)
  58. {
  59. perror ("listen");
  60. exit (EXIT_FAILURE);
  61. }
  62. /* Initialize the set of active sockets. */
  63. FD_ZERO (&active_fd_set);
  64. FD_SET (sock, &active_fd_set);
  65. while (1)
  66. {
  67. /* Block until input arrives on one or more active sockets. */
  68. read_fd_set = active_fd_set;
  69. if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
  70. {
  71. perror ("select");
  72. exit (EXIT_FAILURE);
  73. }
  74. /* Service all the sockets with input pending. */
  75. for (i = 0; i < FD_SETSIZE; ++i)
  76. if (FD_ISSET (i, &read_fd_set))
  77. {
  78. if (i == sock)
  79. {
  80. /* Connection request on original socket. */
  81. int new;
  82. size = sizeof (clientname);
  83. new = accept (sock,
  84. (struct sockaddr *) &clientname,
  85. &size);
  86. if (new < 0)
  87. {
  88. perror ("accept");
  89. exit (EXIT_FAILURE);
  90. }
  91. fprintf (stderr,
  92. "Server: connect from host %s, port %hd.\n",
  93. inet_ntoa (clientname.sin_addr),
  94. ntohs (clientname.sin_port));
  95. FD_SET (new, &active_fd_set);
  96. }
  97. else
  98. {
  99. /* Data arriving on an already-connected socket. */
  100. if (read_from_client (i) < 0)
  101. {
  102. close (i);
  103. FD_CLR (i, &active_fd_set);
  104. }
  105. }
  106. }
  107. }
  108. }