Boost http服务器示例不起作用

Boost http server example not working?

本文关键字:不起作用 服务器 http Boost      更新时间:2023-10-16

我已经从本教程中复制了文件(来自"HTTP服务器"的文件),但它似乎不起作用。我已经使用0.0.0.0 5000 .运行了应用程序,但当我尝试连接到页面localhost:5000时,我总是得到404 Not Found。如何使其运行?

如果您得到一个状态代码为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附加到该路径。如代码中所示,如果服务器未能打开路径指定的文件,则服务器将使用具有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;
}