【发布时间】:2014-09-19 23:11:52
【问题描述】:
我正在使用select() 和NONBLOCKING 连接fd 来接受连接并处理输入和输出。我在处理超过缓冲区大小的大数据传输时遇到了一些问题。所以例如这里:
readCount = 1;
while(readCount > 0)
{
// clear t_input to read new data from the fd
memset(t_input, 0, CLIENT_INPUT_BUFF_LEN);
// read data from the client connectionFd and since t_input is 1024 only
// read that much
readCount = read(iter->connectionFd, t_input, 1024);
printf("<<< %d - %s >>> \n", errno, strerror(errno));
if(readCount == -1)
{
if( errno == EAGAIN)
break;
}
iter->AppendInputData(t_input, readCount);
}
这不适用于大数据。因此,当我传输小于1024 的数据时,第一个read 调用成功完成,数据被复制到AppendInputData 调用中。由于它处于循环中,第二个read 调用返回-1,并将errno 设置为EAGAIN,然后打破循环——这一切都适用于这种情况。
但是,在大于1024 数据的情况下,第二次读取调用再次失败,errno 设置为EAGAIN。奇怪的是,在调试模式下,我看不到这种行为,第二个或第三个 read 调用返回正常,并且收集了所有数据。有人可以解释可能发生的事情吗?
【问题讨论】:
-
你的 select() 调用在哪里?获得 EAGAIN/EWOULDBLOCK 是正常的和预期的,这意味着您的非阻塞 read() 调用将被阻塞。当 select() 说可以时,您应该返回执行 select() 并阅读更多内容。
标签: c linux sockets select file-descriptor