简单回射程序之处理accept返回EINTR错误的服务器程序版本

时间:2022-06-20 10:53:38
#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, ); 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)) < ) {
  if (errno == EINTR) {
continue;
  } else {
  perror("accept");
return;
  }
} if ( (childpid = fork()) == ) { /* Generate child processes to handle foreign connections */
close(listenfd);
str_echo(connfd);
exit();
}
close(connfd);
}
close(listenfd);
exit();
} void str_echo(int sockfd) {
ssize_t n;
char buf[MAXLINE]; again:
while ( (n = read(sockfd, buf, MAXLINE)) > ) {
writen(sockfd, buf, n);
bzero(buf, sizeof(buf));
}
if (n < && errno == EINTR) {
goto again;
} else if (n < ) {
perror("read");
}
}