| 50 | } |
| 51 | |
| 52 | int echo_server() |
| 53 | { |
| 54 | struct sockaddr_in addr; |
| 55 | addr.sin_family = AF_INET; |
| 56 | addr.sin_addr.s_addr = INADDR_ANY; |
| 57 | addr.sin_port = htons(80); |
| 58 | |
| 59 | int fd = create_tcp_sock(); |
| 60 | if (fd < 0) { |
| 61 | fprintf(stderr, "create listen socket failed\n"); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 66 | close(fd); |
| 67 | fprintf(stderr, "bind failed [%m]\n"); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | if (listen(fd, 1024) < 0) { |
| 72 | close(fd); |
| 73 | fprintf(stderr, "listen failed [%m]\n"); |
| 74 | return -1; |
| 75 | } |
| 76 | int clt_fd = 0; |
| 77 | int *p; |
| 78 | while (true) { |
| 79 | struct sockaddr_in client_addr; |
| 80 | int addr_len = sizeof(client_addr); |
| 81 | |
| 82 | clt_fd = mt_accept(fd, (struct sockaddr*)&client_addr, (socklen_t*)&addr_len, -1); |
| 83 | if (clt_fd < 0) { |
| 84 | mt_sleep(1); |
| 85 | continue; |
| 86 | } |
| 87 | if (set_fd_nonblock(clt_fd) == -1) { |
| 88 | fprintf(stderr, "set clt_fd nonblock failed [%m]\n"); |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | p = new int(clt_fd); |
| 93 | mt_start_thread((void *)echo, (void *)p); |
| 94 | } |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | int main(int argc, char *argv[]) |
| 99 | { |
no test coverage detected