【问题标题】:Boost http server example not working?Boost http 服务器示例不起作用?
【发布时间】:2015-04-30 08:15:38
【问题描述】:

我已经从this tutorial 复制了文件(来自“HTTP 服务器”的文件),但它似乎无法正常工作。我已经使用0.0.0.0 5000 . 运行了该应用程序,但是当我尝试连接到页面 localhost:5000 时,我总是得到 404 Not Found。如何让它运行?

【问题讨论】:

  • 如果你绑定到127.0.0.1而不是0.0.0.0怎么办?
  • 您能否更详细地描述一下设置?预期的结果是什么?您要提供什么文件?它位于文件系统的什么位置?您在浏览器中请求的确切 URL 是什么?

标签: c++ http boost boost-asio server


【解决方案1】:

如果您收到状态码为 404 的 HTTP 响应,则表明 HTTP 服务器正在运行、处理请求并提供响应。如果服务器没有运行,则不会返回 HTTP 响应。浏览器可能会提供有关失败的其他详细信息:

$ lsof -i tcp:5000 # verify nothing is listening to port 5000
$ curl http://localhost:5000/
curl: (7) Failed to connect to localhost port 5000: Connection refused

验证HTTP请求中请求的路径是否存在于启动服务器时提供的doc_root参数对应的目录中。另外,请注意,如果请求路径以/ 结尾,那么服务器会将index.html 附加到路径中。如code 中所见,如果服务器无法打开路径指定的文件,则服务器将响应具有 404 状态码的 HTTP 响应。

// Open the file to send back.
std::string full_path = doc_root_ + request_path;
std::ifstream is(full_path.c_str(), std::ios::in | std::ios::binary);
if (!is)
{
  rep = reply::stock_reply(reply::not_found);
  return;
}

【讨论】:

  • 返回:<html><head><title>Not Found</title></head><body><h1>404 Not Found</h1></body></html>
  • @sop 明白了。如果服务器没有运行,那么就没有 HTTP 响应,因为它会在较低的层失败,例如 TCP 传输层。检查 doc_root 是否包含 HTTP 请求中请求的文件。
  • doc_root 在哪里?
  • doc_root 是启动时提供给程序的第三个参数:Usage: http_server <address> <port> <doc_root>
  • @sop 请用 cmets 中问题的答案更新您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-14
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-08
相关资源
最近更新 更多