gpt4 book ai didi

c - curl 套接字挂起

转载 作者:行者123 更新时间:2023-11-30 15:30:37 25 4
gpt4 key购买 nike

上下文

Debian 64 位学习http

问题

我使用curl以这种方式连接到本地主机端口36000 curl 本地主机: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 处停止...而它是非阻塞的(!!!)

因此连接永远不会关闭。

你能告诉我为什么吗?

最佳答案

好吧,是 make non-blocking 把这一切搞砸了。复制粘贴代码并且它可以工作。

谢谢

关于c - curl 套接字挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25526606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com