解决问题:
服务器进程或主机意外崩溃或重启
输出重定向
客户端代码:
vi c3.c
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#define SERV_PORT 9877
#define MAXLINE 4096
void client_echo(FILE *fp,int sockfd)
{
int fd = fileno(fp);
int maxfd = fd > sockfd ? fd : sockfd;
fd_set fs;
FD_ZERO(&fs);
int n;
char buf[MAXLINE];
int ioeof = 0;
for (; ;)
{
if (ioeof == 0)
FD_SET(fd,&fs);
FD_SET(sockfd,&fs);
if (select(maxfd + 1,&fs,NULL,NULL,NULL) < 0)
{
printf("select: %s\n",strerror(errno));
exit(1);
}
if (FD_ISSET(sockfd,&fs))
{
if ((n = read(sockfd,buf,MAXLINE)) == 0)
{
if (ioeof == 1)
return;
else
printf("server is broke\n");
}else if (n < 0)
{
printf("read: %s\n",strerror(errno));