一个poll函数使用的例子

一个poll函数使用的例子:(代码抄自《UNIX网络编程》P147),for循环嵌套很深,例子很一般。此处仅做记录代码使用。

int main(int argc, char **argv)
{   
	INT iConfd = 0;
    INT iReadLen = 0;
    INT iMaxFd = 0;
    INT iMaxIndex = 0;
	struct pollfd astPollfd[BUF_LEN_16];
	INT iIndex = 0;
    INT iReady = 0;
    CHAR szBuf[BUF_LEN_256] = {0,};
    
    /* 注册信号 */
    (VOID)reg_sigal(abnomalExit);

    /* 初始化 */
    process_init();
  
    /* 创建服务器 */
    if(ERROR_SUCCESS != sock_CreateServer(SERVER_PORT, &g_iListenSocket))
    {
        printf("create server failed.\n");
        return -1;
    }
	astPollfd[0].fd = g_iListenSocket;
	astPollfd[0].events = POLLRDNORM;

    for(iIndex = 1; iIndex<BUF_LEN_16; iIndex++)
    {
    	astPollfd[iIndex].fd = -1;
    }
    for(;;)
    {
        iReady = poll(astPollfd, iMaxIndex + 1, -1);
		if(0 != (astPollfd[0].revents & POLLRDNORM))
        {
        	iConfd = accept(g_iListenSocket, NULL, NULL);
            for(iIndex = 1;iIndex < BUF_LEN_16;++iIndex)
            {
            	if(astPollfd[iIndex].fd<0)
                {
                	astPollfd[iIndex].fd = iConfd;
                    break;		/* 跳出内层循环 */
               	}
            }
            if (iIndex == BUF_LEN_16)
            {
                printf("too many client.\n");
				break; /* 跳出外层循环 */
            }
            /* 将链接socket加入监听 */
        	astPollfd[iIndex].events = POLLRDNORM;
            if(iIndex>iMaxIndex)
            {
            	iMaxIndex = iIndex;
            }
            if(--iReady < 0)
            {
            	continue;
            }
        }
        for(iIndex = 1;iIndex <= iMaxIndex; ++iIndex)
        {
        	if(astPollfd[iIndex].fd < 0)
            {
            	continue;
            }
            if(0 != (astPollfd[iIndex].revents & (POLLRDNORM | POLLERR)))
            {
            	if(0 > (iReadLen = read(astPollfd[iIndex].fd, szBuf, BUF_LEN_256)))
                {
                	if(errno == ECONNRESET)
                    {
                    	close(astPollfd[iIndex].fd);
						astPollfd[iIndex].fd=-1;               
                    }
                    else
                    {
                        return -1;
                    }
                }
                else if(0 == iReadLen)
                {
                	close(astPollfd[iIndex].fd);
					astPollfd[iIndex].fd=-1;               
                }
                else
                {
                	printf("%s",szBuf);
                    write(astPollfd[iIndex].fd, szBuf, iReadLen);
                }
                if(--iReady<=0)
                {
                	break;
                }
            }
       	}
    }  
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用 `poll` 函数连续监测多个文件描述符的简单示例: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <poll.h> #define MAX_EVENTS 2 int main() { struct pollfd fds[MAX_EVENTS]; int timeout = 5000; // 超时时间,单位为毫秒 // 打开两个文件描述符 int fd1 = open("file1.txt", O_RDONLY); int fd2 = open("file2.txt", O_RDONLY); // 设置文件描述符和事件 fds[0].fd = fd1; fds[0].events = POLLIN; fds[1].fd = fd2; fds[1].events = POLLIN; while (1) { int ret = poll(fds, MAX_EVENTS, timeout); if (ret == -1) { perror("poll"); exit(EXIT_FAILURE); } else if (ret == 0) { printf("Timeout occurred.\n"); } else { for (int i = 0; i < MAX_EVENTS; i++) { if (fds[i].revents & POLLIN) { printf("File descriptor %d has data to read.\n", fds[i].fd); char buffer[1024]; ssize_t bytesRead = read(fds[i].fd, buffer, sizeof(buffer)); if (bytesRead == -1) { perror("read"); exit(EXIT_FAILURE); } printf("Read %zd bytes: %s\n", bytesRead, buffer); } } } } // 关闭文件描述符 close(fd1); close(fd2); return 0; } ``` 在上面的示例中,我们使用 `poll` 函数来监测两个文件描述符 `fd1` 和 `fd2` 是否有数据可读。首先,我们打开了两个文件描述符并设置对应的事件为 `POLLIN`,然后进入一个无限循环。在循环中,调用 `poll` 函数来等待事件发生,如果有事件发生,则通过检查 `revents` 域来确定是哪个文件描述符上有数据可读。然后,我们使用 `read` 函数从文件中读取数据并进行处理。 这个示例展示了如何使用 `poll` 函数来连续监测多个文件描述符的状态,并在有数据可读时进行相应的处理。需要注意的是,示例中的超时时间设置为 5 秒,可以根据需要进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值