1. 异常描述
a. client 连接到server之后,server fork一个child,正常通信
b. kill child,这会导致
TCP:
server--->FIN-->client
client--->ACK-->server
c. Server内部
SIGCHLD --> parent --> waitpid --> 子进程终止
d. Clienet内部
一直阻塞在fgets函数上,不知道server己经终止
e. 一句话总结问题
当client阻塞在fgets函数上时,socket有情况,fgets只能处理用户输入处理不了socket,导致异常
2. 源码
client.c
-
cong@msi:/work/test/tcpip/11select/client$ cat client.c
-
#include "utils.h"
-
-
//#define TIME_SERV_ADDR "192.168.4.98"
-
#define TIME_SERV_ADDR "127.0.0.1"
-
-
ssize_t readline(int fd, void* vptr, size_t maxlen)
-
{
-
ssize_t n, rc;
-
char c,*ptr;
-
ptr = vptr;
-
for(n=0; n<maxlen; n++)
-
{
-
again:
-
if( (rc=read(fd,&c,1)) == 1) //readline实际上是readb阻塞
-
{
-
*ptr++ = c;
-
if(c == '\n')
-
break;
-
} else if(rc == 0) {
-
*ptr = 0;
-
return n;
-
}else {
-
if( errno == EINTR)
-
goto again;
-
return -1;
-
}
-
}
-
*ptr = '\0';
-
return n;
-
}
-
-
#if 0
-
void str_cli(FILE* fp, int sockfd)
-
{
-
char sendline[MAXLINE], recvline[MAXLINE];
-
while(fgets(sendline, MAXLINE, fp) != NULL) //阻塞在fgets上时,只能处理stdin,当socket再有变化时,就搞不定了
-
{
-
write(sockfd, sendline, strlen(sendline));
-
-
if(readline(sockfd, recvline, MAXLINE) == 0)
-
dbmsg("server terminated");
-
fputs(recvline, stdout);
-
}
-
-
}
-
#endif
-
void str_cli(FILE* fp, int sockfd)
-
{
-
int maxfdp1,ret;
-
fd_set rset;
-
char sendline[MAXLINE], recvline[MAXLINE];
-
FD_ZERO(&rset);
-
while(1)
-
{
-
FD_SET(fileno(fp), &rset);
-
FD_SET(sockfd, &rset);
-
maxfdp1 = max(fileno(fp), sockfd)+1;
-
//dbmsg("fd=%d,sockfd=%d,maxfdp1=%d",fileno(fp), sockfd, maxfdp1);
-
ret = select(maxfdp1, &rset, NULL, NULL, NULL); //改进程序,一直轮询stdin与socket,哪个有变化就处理哪个
-
if(ret <= 0)
-
continue;
-
if(FD_ISSET(sockfd, &rset)) //socket有变化就处理socket
-
{
-
if( (readline(sockfd, recvline, MAXLINE)) == 0)
-
dbmsg("server terminated");
-
fputs(recvline, stdout);
-
}
-
if(FD_ISSET(fileno(fp),&rset)) //stdin有变化就处理stdin
-
{
-
if(fgets(sendline, MAXLINE, fp) == NULL)
-
return;
-
write(sockfd, sendline, strlen(sendline));
-
}
-
}
-
}
-
-
int main ( int argc, char *argv[] )
-
{
-
int i;
-
int sockfd, n;
-
struct sockaddr_in servaddr;
-
char recvline[1024];
-
sockfd = socket(AF_INET, SOCK_STREAM, 0);
-
if(sockfd < 0)
-
return -1;
-
bzero(&servaddr, sizeof(servaddr));
-
servaddr.sin_family = AF_INET;
-
servaddr.sin_port = htons(13999);
-
inet_pton(AF_INET, TIME_SERV_ADDR, &servaddr.sin_addr);
-
if( connect(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr))< 0)
-
return -1;
-
dbmsg("connect success");
-
str_cli(stdin, sockfd);
-
return EXIT_SUCCESS;
-
}
3. 附件
serv.rar (下载后改名为server.tar.gz)
client.rar (下载后改名为client.tar.gz)
阅读(1120) | 评论(0) | 转发(0) |