1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/select.h> #include <sys/socket.h> #include <arpa/inet.h>
#define MAXLINE 255
int open_fd(in_port_t port) { struct sockaddr_in address; address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(port); int fd = socket(AF_INET, SOCK_STREAM, 0); int err = bind(fd, (struct sockaddr *)&address, sizeof(address)); if (err == -1) { printf("[%s] - (%s:%d) error bind %d", __FILE__, __FUNCTION__ , __LINE__, port); exit(0); } return fd; }
void command() { char buf[MAXLINE]; if (!fgets(buf, MAXLINE, stdin)) { exit(0); } printf("%s", buf); }
void echo(int conn) { char buf[MAXLINE]; ssize_t len = recv(conn, buf, sizeof(buf), 0); buf[len] = '\0'; fprintf(stdout, "%s", buf); send(conn, buf, sizeof(buf), 0); }
int main(int argc, char **argv) { int listenfd, connfd; socklen_t client_len; struct sockaddr_storage client_addr; fd_set read_set, ready_set; if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(0); } listenfd = open_fd(atoi(argv[1])); FD_ZERO(&read_set); FD_SET(STDIN_FILENO, &read_set); FD_SET(listenfd, &read_set);
while (1) { ready_set = read_set; select(listenfd + 1, &ready_set, NULL, NULL, NULL); if (FD_ISSET(STDIN_FILENO, &ready_set)) { command(); }
if (FD_ISSET(listenfd, &ready_set)) { client_len = sizeof(struct sockaddr_storage); connfd = accept(listenfd, (struct sockaddr *)&client_addr, &client_len); echo(connfd); close(listenfd); } }
return 0; }
|