您的位置:首页 > 理论基础 > 计算机网络

利用select和shutdown的str_cli函数

2015-05-04 12:52 162 查看
void str_cli(FILE *fp, int sockfd)
{
int maxfdp1, stdineof;
fd_set rset;
char buf[MAXLINE];
int n;

stdineof = 0;
FD_ZERO(&rset);
for( ; ; )
{
if(stdineof == 0)
FD_SET(fileno(fp), &rset);
FD_SET(sockfd, &rset);
maxfdp1 = max(fileno(fp), sockfd) + 1;
Select(maxfdp1, &rset, NULL, NULL, NULL);

if(FD_ISSET(sockfd, &rset))	/* socket is readable */
{
if((n = Read(sockfd, buf, MAXLINE)) == 0)
{
if(stdineof == 1)
return;			/* nomal termination*/
else
err_quit("str_cli: server terminated ");
}
Write(fileno(stdout), buf, n);
}
if(FD_ISSET(fileno(fp), &rset))	/* input is readable */
{
if((n = Read(fileno(fp), buf, MAXLINE)) == 0)
{
stdineof = 1;
Shutdown(sockfd, SHUT_WR);	/* send FIN */
FD_CLR(fileno(fp), &rset);
continue;
}
Writen(sockfd, buf, n);
}
}
}

str_cli函数,利用了select函数使客户端可以响应关闭服务器端连接,shutdown函数可以允许正确的批量输入。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐