#include #include #include #include #include void handle_client(int c) { char buf[8192]; char *lastpos; int size; while (1) { size = recv(c, buf, 8192, 0); if (size == 0) { break; } lastpos = strchr(buf, '\n'); send(c, buf, lastpos+1-buf, 0); } } int main() { int s, c; int reuseaddr = 1; struct sockaddr_in6 addr; int pid; s = socket(AF_INET6, SOCK_STREAM, 0); setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr)); addr.sin6_family = AF_INET6; addr.sin6_port = htons(5000); addr.sin6_addr = in6addr_any; bind(s, (struct sockaddr *)&addr, sizeof(addr)); listen(s, 5); while (1) { c = accept(s, NULL, NULL); pid = fork(); if (pid == -1) { exit(1); } else if (pid == 0) { close(s); handle_client(c); close(c); return 0; } else { close(c); waitpid(pid, NULL, 0); } } }