#include <stdio.h> #include <stdlib.h> #include <time.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <unistd.h> #include <signal.h> #include <sys/socket.h> #define SERV_PORT 9999 #define MAXLINE 4096 #define LISTENQ 1024 #define SA struct sockaddr typedef void Sigfunc(int); void str_echo(int); void sig_chld(int); ssize_t writen(int, const void*, size_t); Sigfunc *signal(int, Sigfunc*); int main(int argc, char *argv[]) { int listenfd, connfd; pid_t childpid; socklen_t clilen; struct sockaddr_in cliaddr, servaddr; listenfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); bind(listenfd, (SA *)&servaddr, sizeof(servaddr)); listen(listenfd, LISTENQ); signal(SIGCHLD, sig_chld); /* Install signal processing function */ for ( ; ; ) { clilen = sizeof(cliaddr); if ( (connfd = accept(listenfd, (SA *)&cliaddr, &clilen)) < 0) { if (errno == EINTR) { continue; } else { perror("accept"); return; } } if ( (childpid = fork()) == 0) { /* Generate child processes to handle foreign connections */ close(listenfd); str_echo(connfd); exit(0); } close(connfd); } close(listenfd); exit(0); } void str_echo(int sockfd) { ssize_t n; char buf[MAXLINE]; again: while ( (n = read(sockfd, buf, MAXLINE)) > 0) { writen(sockfd, buf, n); bzero(buf, sizeof(buf)); } if (n < 0 && errno == EINTR) { goto again; } else if (n < 0) { perror("read"); } }