📄 tcpechod.c
字号:
/* TCPechod.c - main, TCPechod */#define _USE_BSD#include <sys/types.h>#include <sys/signal.h>#include <sys/socket.h>#include <sys/time.h>#include <sys/resource.h>#include <sys/wait.h>#include <sys/errno.h>#include <netinet/in.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#define QLEN 32 /* maximum connection queue length */#define BUFSIZE 4096extern int errno;void reaper(int);int TCPechod(int fd);int errexit(const char *format, ...);int passiveTCP(const char *service, int qlen);/*------------------------------------------------------------------------ * main - Concurrent TCP server for ECHO service *------------------------------------------------------------------------ */intmain(int argc, char *argv[]){ char *service = "echo"; /* service name or port number */ struct sockaddr_in fsin; /* the address of a client */ unsigned int alen; /* length of client's address */ int msock; /* master server socket */ int ssock; /* slave server socket */ switch (argc) { case 1: break; case 2: service = argv[1]; break; default: errexit("usage: TCPechod [port]\n"); } msock = passiveTCP(service, QLEN); (void) signal(SIGCHLD, reaper); while (1) { alen = sizeof(fsin); ssock = accept(msock, (struct sockaddr *)&fsin, &alen); if (ssock < 0) { if (errno == EINTR) continue; errexit("accept: %s\n", strerror(errno)); } switch (fork()) { case 0: /* child */ (void) close(msock); exit(TCPechod(ssock)); default: /* parent */ (void) close(ssock); break; case -1: errexit("fork: %s\n", strerror(errno)); } }}/*------------------------------------------------------------------------ * TCPechod - echo data until end of file *------------------------------------------------------------------------ */intTCPechod(int fd){ char buf[BUFSIZ]; int cc; while (cc = read(fd, buf, sizeof buf)) { if (cc < 0) errexit("echo read: %s\n", strerror(errno)); if (write(fd, buf, cc) < 0) errexit("echo write: %s\n", strerror(errno)); } return 0;}/*------------------------------------------------------------------------ * reaper - clean up zombie children *------------------------------------------------------------------------ */voidreaper(int sig){ int status; while (wait3(&status, WNOHANG, (struct rusage *)0) >= 0) /* empty */;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -