【发布时间】:2014-08-27 11:59:09
【问题描述】:
上下文
Debian 64 位 学习http
问题
我使用 curl 以这种方式连接到 localhost 端口 36000 卷曲 localhost:36000 -v
我在这里复制粘贴代码 non blocking socket
这是我的 else 替代品:
else
{
int done = 0;
while ( 1 )
{
printf("yy %d\n",i);
ssize_t count;
char buf[210] = {0};
/* it will block here at the second iteration */
count = read ( events[i].data.fd, buf, sizeof buf );
printf("count = %d\n",count);
if ( count == -1 )
{
/* If errno == EAGAIN, that means we have read all
data. So go back to the main loop. */
if ( errno != EAGAIN )
{
perror ( "fini\n" );
}
break;
}
else if ( count == 0 )
{
/* End of file. The remote has closed the
connection. */
done = 1;
break;
}
printf("buf ? %s\n\n\n\n",buf);
char resp[] = "HTTP/1.1 200 OK\r\nContent-Length: 103\r\nContent-Type: text/html\r\n\r\n<html><body>you</body></html>\n";
printf("buf2 ? %s\n",resp);
sentinel = write ( events[i].data.fd, resp, strlen ( resp ) + 1);
if ( sentinel == -1 )
{
printf("err\n");
//exit ( 1 );
}
}
if ( done )
{
/* Closing the descriptor will make epoll remove it
from the set of descriptors which are monitored. */
printf("out\n");
close ( events[i].data.fd );
}
}
}
问题
问题是 curl 挂起,服务器在第二次迭代读取之前等待。
我的意思是它读取一次然后将响应写入 fd,然后在 while 的顶部再次返回,但在 count = read 处停止...而它是非阻塞的 (!!!)
因此连接永远不会关闭。
你能告诉我为什么吗?
【问题讨论】: