1. #include <pthread.h>
    2. #include <signal.h>
    3. #include "hl.h"
    4. void cli_data_handle (void *arg);
    5. void sig_child_handle(int signo)
    6. {
    7. if(SIGCHLD == signo) {
    8. waitpid(-1, NULL, WNOHANG);
    9. }
    10. }
    11. int main (void)
    12. {
    13. int fd = -1;
    14. struct sockaddr_in sin;
    15. signal(SIGCHLD, sig_child_handle);
    16. /* 1. 创建socket fd */
    17. if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
    18. perror ("socket");
    19. exit (1);
    20. }
    21. /*优化4: 允许绑定地址快速重用 */
    22. int b_reuse = 1;
    23. setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));
    24. /*2. 绑定 */
    25. /*2.1 填充struct sockaddr_in结构体变量 */
    26. bzero (&sin, sizeof (sin));
    27. sin.sin_family = AF_INET;
    28. sin.sin_port = htons (SERV_PORT); //网络字节序的端口号
    29. /*优化1: 让服务器程序能绑定在任意的IP上 */
    30. #if 1
    31. sin.sin_addr.s_addr = htonl (INADDR_ANY);
    32. #else
    33. if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
    34. perror ("inet_pton");
    35. exit (1);
    36. }
    37. #endif
    38. /*2.2 绑定 */
    39. if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
    40. perror ("bind");
    41. exit (1);
    42. }
    43. /*3. 调用listen()把主动套接字变成被动套接字 */
    44. if (listen (fd, BACKLOG) < 0) {
    45. perror ("listen");
    46. exit (1);
    47. }
    48. printf ("Server starting....OK!\n");
    49. int newfd = -1;
    50. /*4. 阻塞等待客户端连接请求 */
    51. struct sockaddr_in cin;
    52. socklen_t addrlen = sizeof (cin);
    53. while(1) {
    54. pid_t pid = -1;
    55. if ((newfd = accept (fd, (struct sockaddr *) &cin, &addrlen)) < 0) {
    56. perror ("accept");
    57. break;
    58. }
    59. /*创建一个子进程用于处理已建立连接的客户的交互数据*/
    60. if((pid = fork()) < 0) {
    61. perror("fork");
    62. break;
    63. }
    64. if(0 == pid) { //子进程中
    65. close(fd);
    66. char ipv4_addr[16];
    67. if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
    68. perror ("inet_ntop");
    69. exit (1);
    70. }
    71. printf ("Clinet(%s:%d) is connected!\n", ipv4_addr, ntohs(cin.sin_port));
    72. cli_data_handle(&newfd);
    73. return 0;
    74. } else { //实际上此处 pid >0, 父进程中
    75. close(newfd);
    76. }
    77. }
    78. close (fd);
    79. return 0;
    80. }
    81. void cli_data_handle (void *arg)
    82. {
    83. int newfd = *(int *) arg;
    84. printf ("Child handling process: newfd =%d\n", newfd);
    85. //..和newfd进行数据读写
    86. int ret = -1;
    87. char buf[BUFSIZ];
    88. char resp_buf[BUFSIZ+10];
    89. while (1) {
    90. bzero (buf, BUFSIZ);
    91. do {
    92. ret = read (newfd, buf, BUFSIZ - 1);
    93. } while (ret < 0 && EINTR == errno);
    94. if (ret < 0) {
    95. perror ("read");
    96. exit (1);
    97. }
    98. if (!ret) { //对方已经关闭
    99. break;
    100. }
    101. printf ("Receive data: %s\n", buf);
    102. bzero(resp_buf,BUFSIZ+10);
    103. strncpy(resp_buf,SERV_RESP_STR,strlen(SERV_RESP_STR));
    104. strcat(resp_buf,buf);
    105. do{
    106. write(newfd,resp_buf,strlen(resp_buf));
    107. }while(ret<0 && EINTR ==errno);
    108. if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
    109. printf ("Client(fd=%d) is exiting!\n", newfd);
    110. break;
    111. }
    112. bzero(resp_buf, BUFSIZ+10);
    113. strncpy(resp_buf, SERV_RESP_STR, strlen(SERV_RESP_STR));
    114. strcat(resp_buf, buf);
    115. do {
    116. ret = write(newfd, resp_buf, strlen(resp_buf));
    117. }while(ret < 0 && EINTR == errno);
    118. }
    119. close (newfd);
    120. }