首页 > 其他 > 详细

使用/dev/poll的str_cli函数

时间:2019-04-24 19:48:40      阅读:159      评论:0      收藏:0      [点我收藏+]
void str_cli(FILE *fp, int sockfd)
{
    int        stdineof;
    char       buf[MAXLINE];
    int        n;
    int        wfd;
    struct pollfd    pollfd[2];
    struct dvpoll    dopoll;
    int        i;
    int        result;

    wfd = open("/dev/poll", O_RDWR, 0);

    pollfd[0].fd = fileno(fp);
    pollfd[0].events = POLLIN;
    pollfd[0].revents = 0;

    pollfd[1].fd = sockfd;
    pollfd[1].events = POLLIN;
    pollfd[1].revents = 0;

    write(wfd, pollfd, sizeof(struct pollfd) * 2);

    stdineof = 0;
    for ( ; ; ) {
        /* block until /dev/poll says something is ready */
        dopoll.dp_timeout = -1;
        dopoll.dp_nfds = 2;
        dopoll.dp_fds = pollfd;
        result = ioctl(wfd, DP_POLL, &dopoll);

        /* loop through ready file descriptors */
        for (i = 0; i < result; i++) {
            if (dopoll.dp_fds[i].fd == sockfd) {
                /* socket is readable */
                if ( (n = read(sockfd, buf, MAXLINE)) == 0) {
                    if (stdineof == 1)
                        return;        /* normal termination */
                    else
                        err_quit("str_cli: server terminated prematurely");
                }

                write(fileno(stdout), buf, n);
            } else {
                /* input is readable */
                if ( (n = read(fileno(fp), buf, MAXLINE)) == 0) {
                    stdineof = 1;
                    shutdown(sockfd, SHUT_WR);    /* send FIN */
                    continue;
                }

                writen(sockfd, buf, n);
            }
        }
    }
}

 

使用/dev/poll的str_cli函数

原文:https://www.cnblogs.com/soldierback/p/10764522.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!