【问题标题】:c - curl socket hangsc - curl 套接字挂起
【发布时间】:2014-08-27 11:59:09
【问题描述】:

上下文

Debian 64 位 学习http

问题

我使用 curl 以这种方式连接到 localhost 端口 36000 卷曲 l​​ocalhost: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 处停止...而它是非阻塞的 (!!!)

因此连接永远不会关闭。

你能告诉我为什么吗?

【问题讨论】:

    标签: c sockets curl freeze


    【解决方案1】:

    好的,这是 make non-blocking 把它搞砸了。复制粘贴代码就可以了。

    谢谢

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 2019-10-14
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      • 2015-09-13
      相关资源
      最近更新 更多