使用/dev/poll的str_cli函数

时间:2023-03-09 19:35:23
使用/dev/poll的str_cli函数
void str_cli(FILE *fp, int sockfd)
{
int stdineof;
char buf[MAXLINE];
int n;
int wfd;
struct pollfd pollfd[];
struct dvpoll dopoll;
int i;
int result; wfd = open("/dev/poll", O_RDWR, ); pollfd[].fd = fileno(fp);
pollfd[].events = POLLIN;
pollfd[].revents = ; pollfd[].fd = sockfd;
pollfd[].events = POLLIN;
pollfd[].revents = ; write(wfd, pollfd, sizeof(struct pollfd) * ); stdineof = ;
for ( ; ; ) {
/* block until /dev/poll says something is ready */
dopoll.dp_timeout = -;
dopoll.dp_nfds = ;
dopoll.dp_fds = pollfd;
result = ioctl(wfd, DP_POLL, &dopoll); /* loop through ready file descriptors */
for (i = ; i < result; i++) {
if (dopoll.dp_fds[i].fd == sockfd) {
/* socket is readable */
if ( (n = read(sockfd, buf, MAXLINE)) == ) {
if (stdineof == )
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)) == ) {
stdineof = ;
shutdown(sockfd, SHUT_WR); /* send FIN */
continue;
} writen(sockfd, buf, n);
}
}
}
}