2017-03-02 45 views
0

我想用mongoose C库开发一个服务器应用程序。在我尝试的初始阶段,我坚持发送HTTP请求的响应。我想送status 200一个简单的响应使用的代码下面一行:mongoose mg_send_response_line()不工作

mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *"); 
printf("Response sent...\n"); 

但反应未接收到客户端(邮差或web浏览器)。 没有错误,甚至是printf line of Response sent is printed。 作为针对这一点,以下行得到成功执行:

mg_http_send_error(nc,404, "Fatal Error!"); // I get this error at client side. 

基本simplest_web_server也能正常工作。为什么我的单行代码发送响应失败。我无法理解/调试它。

Regards,

Neeraj。

回答

0

问题是没有为HTTP响应指定内容长度或传输编码,并且服务器未关闭连接,因此客户端挂起等待响应。

如果你去翻source code,你会看到,在mg_http_send_error(),该MG_F_SEND_AND_CLOSE标志设置,但它不是内mg_send_response_line()设置(虽然,像你这样的,我认为这将通过函数处理)。

要解决在上下文中的问题,

mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *"); 
printf("Response sent...\n"); 
nc->flag |= MG_F_SEND_AND_CLOSE;