1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <unistd.h>
    4. #include <errno.h>
    5. #include <string.h>
    6. #include <sys/types.h>
    7. #include <sys/socket.h>
    8. #include <netinet/in.h>
    9. #include <arpa/inet.h>
    10. #define MYPORT 88960 // the port users will be connecting to
    11. #define BACKLOG 10 // how many pending connections queue will hold
    12. #define BUF_SIZE 200
    13. int fd_A[BACKLOG]; // accepted connection fd
    14. int conn_amount; // current connection amount
    15. void showclient()
    16. {
    17. int i;
    18. printf("client amount: %d\n", conn_amount);
    19. for (i = 0; i < BACKLOG; i++) {
    20. printf("[%d]:%d ", i, fd_A[i]);
    21. }
    22. printf("\n\n");
    23. }
    24. int main(void)
    25. {
    26. int sock_fd, new_fd; // listen on sock_fd, new connection on new_fd
    27. struct sockaddr_in server_addr; // server address information
    28. struct sockaddr_in client_addr; // connector's address information
    29. socklen_t sin_size;
    30. int yes = 1;
    31. char buf[BUF_SIZE];
    32. int ret;
    33. int i;
    34. if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    35. perror("socket");
    36. exit(1);
    37. }
    38. if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
    39. perror("setsockopt");
    40. exit(1);
    41. }
    42. server_addr.sin_family = AF_INET; // host byte order
    43. server_addr.sin_port = htons(MYPORT); // short, network byte order
    44. server_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
    45. memset(server_addr.sin_zero, '\0', sizeof(server_addr.sin_zero));
    46. if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
    47. perror("bind");
    48. exit(1);
    49. }
    50. if (listen(sock_fd, BACKLOG) == -1) {
    51. perror("listen");
    52. exit(1);
    53. }
    54. printf("listen port %d\n", MYPORT);
    55. fd_set fdsr;
    56. int maxsock;
    57. struct timeval tv;
    58. conn_amount = 0;
    59. sin_size = sizeof(client_addr);
    60. maxsock = sock_fd;
    61. while (1) {
    62. // initialize file descriptor set
    63. FD_ZERO(&fdsr);
    64. FD_SET(sock_fd, &fdsr);
    65. // timeout setting
    66. tv.tv_sec = 30;
    67. tv.tv_usec = 0;
    68. // add active connection to fd set
    69. for (i = 0; i < BACKLOG; i++) {
    70. if (fd_A[i] != 0) {
    71. FD_SET(fd_A[i], &fdsr);
    72. }
    73. }
    74. ret = select(maxsock + 1, &fdsr, NULL, NULL, &tv);
    75. if (ret < 0) {
    76. perror("select");
    77. break;
    78. } else if (ret == 0) {
    79. printf("timeout\n");
    80. continue;
    81. }
    82. // check every fd in the set
    83. for (i = 0; i < conn_amount; i++) {
    84. if (FD_ISSET(fd_A[i], &fdsr)) {
    85. ret = recv(fd_A[i], buf, sizeof(buf), 0);
    86. char str[] = "Good,very nice!\n";
    87. send(fd_A[i],str,sizeof(str) + 1, 0);
    88. if (ret <= 0) { // client close
    89. printf("client[%d] close\n", i);
    90. close(fd_A[i]);
    91. FD_CLR(fd_A[i], &fdsr);
    92. fd_A[i] = 0;
    93. } else { // receive data
    94. if (ret < BUF_SIZE)
    95. memset(&buf[ret], '\0', 1);
    96. printf("client[%d] send:%s\n", i, buf);
    97. }
    98. }
    99. }
    100. // check whether a new connection comes
    101. if (FD_ISSET(sock_fd, &fdsr)) {
    102. new_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &sin_size);
    103. if (new_fd <= 0) {
    104. perror("accept");
    105. continue;
    106. }
    107. // add to fd queue
    108. if (conn_amount < BACKLOG) {
    109. fd_A[conn_amount++] = new_fd;
    110. printf("new connection client[%d] %s:%d\n", conn_amount,
    111. inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
    112. if (new_fd > maxsock)
    113. maxsock = new_fd;
    114. }
    115. else {
    116. printf("max connections arrive, exit\n");
    117. send(new_fd, "bye", 4, 0);
    118. close(new_fd);
    119. break;
    120. }
    121. }
    122. showclient();
    123. }
    124. // close other connections
    125. for (i = 0; i < BACKLOG; i++) {
    126. if (fd_A[i] != 0) {
    127. close(fd_A[i]);
    128. }
    129. }
    130. exit(0);
    131. }