在unix网络编程中的TCP客户/服务器程序示例中第一个示例是使用多进程的阻塞I/O设计的服务器,这里把要注意的地方做一下笔记。
服务器端源码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <netinet/in.h>
#include <errno.h>
#include <arpa/inet.h>
#include <string.h>
#include <signal.h>
void sig_chld(int signo)
{
pid_t pid;
int stat;
while( (pid = waitpid(-1, &stat, WNOHANG)) > 0 )
{
printf("child %d terminated\n", pid);
}
return ;
}
void str_echo(int sockfd)
{
ssize_t n;
char buff[50000];
again:
while( (n = read(sockfd, buff ,sizeof(buff))) > 0 )
{
write(sockfd, buff, n);
}
if(n <0 && errno == EINTR)
goto again;
else if(n < 0)
perror(&#