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. //set time over
    39. struct timeval tv;
    40. tv.tv_sec = 10;
    41. tv.tv_usec = 0;
    42. setsockopt(fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv));
    43. /*2.2 绑定 */
    44. if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
    45. perror ("bind");
    46. exit (1);
    47. }
    48. /*3. 调用listen()把主动套接字变成被动套接字 */
    49. if (listen (fd, BACKLOG) < 0) {
    50. perror ("listen");
    51. exit (1);
    52. }
    53. printf ("Server starting....OK!\n");
    54. int newfd = -1;
    55. /*4. 阻塞等待客户端连接请求 */
    56. struct sockaddr_in cin;
    57. socklen_t addrlen = sizeof (cin);
    58. while(1) {
    59. pid_t pid = -1;
    60. if ((newfd = accept (fd, (struct sockaddr *) &cin, &addrlen)) < 0) {
    61. perror ("accept");
    62. break;
    63. }
    64. /*创建一个子进程用于处理已建立连接的客户的交互数据*/
    65. if((pid = fork()) < 0) {
    66. perror("fork");
    67. break;
    68. }
    69. if(0 == pid) { //子进程中
    70. close(fd);
    71. char ipv4_addr[16];
    72. if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
    73. perror ("inet_ntop");
    74. exit (1);
    75. }
    76. printf ("Clinet(%s:%d) is connected!\n", ipv4_addr, ntohs(cin.sin_port));
    77. cli_data_handle(&newfd);
    78. return 0;
    79. } else { //实际上此处 pid >0, 父进程中
    80. close(newfd);
    81. }
    82. }
    83. close (fd);
    84. return 0;
    85. }
    86. void cli_data_handle (void *arg)
    87. {
    88. int newfd = *(int *) arg;
    89. printf ("Child handling process: newfd =%d\n", newfd);
    90. //..和newfd进行数据读写
    91. int ret = -1;
    92. char buf[BUFSIZ];
    93. char resp_buf[BUFSIZ+10];
    94. while (1) {
    95. bzero (buf, BUFSIZ);
    96. do {
    97. ret = read (newfd, buf, BUFSIZ - 1);
    98. } while (ret < 0 && EINTR == errno);
    99. if (ret < 0) {
    100. perror ("read");
    101. exit (1);
    102. }
    103. if (!ret) { //对方已经关闭
    104. break;
    105. }
    106. printf ("Receive data: %s\n", buf);
    107. bzero(resp_buf,BUFSIZ+10);
    108. strncpy(resp_buf,SERV_RESP_STR,strlen(SERV_RESP_STR));
    109. strcat(resp_buf,buf);
    110. do{
    111. write(newfd,resp_buf,strlen(resp_buf));
    112. }while(ret<0 && EINTR ==errno);
    113. if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
    114. printf ("Client(fd=%d) is exiting!\n", newfd);
    115. break;
    116. }
    117. bzero(resp_buf, BUFSIZ+10);
    118. strncpy(resp_buf, SERV_RESP_STR, strlen(SERV_RESP_STR));
    119. strcat(resp_buf, buf);
    120. do {
    121. ret = write(newfd, resp_buf, strlen(resp_buf));
    122. }while(ret < 0 && EINTR == errno);
    123. }
    124. close (newfd);
    125. }