1. 异常描述
1.1
a. client 连接到server之后,server fork一个child,正常通信
b. 将client的输入重定向
cat client.c | ./client > t
client--->一直发送数据并等侍应答-->当最后一个数据发送完成后-->关闭读写通道
server--->当client关闭读写通道时,由于网络有延迟&&应答确认
-->导致通道的数据没有来得及发送到client端就被丢弃了
c. 解决方法用shutdown来关闭
1.2 缓冲机制
这个还没有看明白,为什么fgets会导致丢数据,有弄明白的大神,让我好好请教一下!!
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)
-
{
-
*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)
-
{
-
write(sockfd, sendline, strlen(sendline));
-
-
if(readline(sockfd, recvline, MAXLINE) == 0)
-
dbmsg("server terminated");
-
fputs(recvline, stdout);
-
}
-
-
}
-
#endif
-
#if 1 //第1次改动
-
void str_cli(FILE* fp, int sockfd)
-
{
-
int maxfdp1,ret, stdineof;
-
fd_set rset;
-
char sendline[MAXLINE], recvline[MAXLINE];
-
FD_ZERO(&rset);
-
while(1)
-
{
-
if(stdineof == 0) //2.3当发送完成后,标志位=1,也就不把写端加入到select队列中了
-
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);
-
if(ret <= 0)
-
continue;
-
if(FD_ISSET(sockfd, &rset))
-
{
-
if( (readline(sockfd, recvline, MAXLINE)) == 0)
-
{
-
if(stdineof == 1)
-
return ;
-
else
-
dbmsg("server terminated");
-
}
-
fputs(recvline, stdout);
-
}
-
if(FD_ISSET(fileno(fp),&rset))
-
{
-
if(fgets(sendline, MAXLINE, fp) == NULL)
-
{
-
stdineof = 1; //2.1当发送完成后,置位标志位
-
shutdown(sockfd, SHUT_WR); //2.2当发送完成后,关闭write端
-
FD_CLR(fileno(fp), &rset); //2.3当发送完成后,将写的fd从select队列中剔除
-
continue;
-
}
-
write(sockfd, sendline, strlen(sendline));
-
}
-
}
-
}
-
#endif
-
-
#if 0 //第2次改动
-
void str_cli(FILE* fp, int sockfd)
-
{
-
int maxfdp1,ret;
-
int stdineof = 0;
-
fd_set rset;
-
//char sendline[MAXLINE], recvline[MAXLINE];
-
char buf[MAXLINE];
-
int n;
-
FD_ZERO(&rset);
-
while(1)
-
{
-
if(stdineof == 0)
-
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);
-
if(ret <= 0)
-
continue;
-
if(FD_ISSET(sockfd, &rset))
-
{
-
//if( (readline(sockfd, recvline, MAXLINE)) == 0)
-
if( (n=read(sockfd, buf, MAXLINE)) == 0)
-
{
-
if(stdineof == 1)
-
return ;
-
else
-
dbmsg("server terminated");
-
}
-
write(fileno(stdout),buf, n);
-
}
-
if(FD_ISSET(fileno(fp),&rset))
-
{
-
//if(fgets(sendline, MAXLINE, fp) == NULL)
-
if( (n=read(fileno(fp), buf, MAXLINE)) == 0)
-
{
-
stdineof = 1;
-
shutdown(sockfd, SHUT_WR);
-
FD_CLR(fileno(fp), &rset);
-
continue;
-
}
-
//write(sockfd, sendline, strlen(sendline));
-
write(sockfd, buf, n);
-
}
-
}
-
}
-
#endif
-
-
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;
-
}
client.rar (下载后改名为client.tar.gz)
serv.rar (下载后改名为serv.tar.gz)
阅读(1064) | 评论(0) | 转发(0) |