C++ 错误:解决:找不到主机(权威)boost beast

C++ 错误:解决:找不到主机(权威)boost beast,c++,boost,localhost,boost-beast,beast,C++,Boost,Localhost,Boost Beast,Beast,我正在学习使用boost beast库。我已经写了服务器。它工作得很好。我还有一个客户端发送POST请求。我正在启动服务器。但是,我无法向他发送URL请求http://localhost:8080,但作为响应,控制台输出:错误:解析:找不到主机(权威) 代码基于以下内容编写: // //由Evgenii Grigorev于2020年11月8日创建。 // #ifndef客户端\u客户端\u水电站 #定义客户端\u客户端\u水电站 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括

我正在学习使用boost beast库。我已经写了服务器。它工作得很好。我还有一个客户端发送POST请求。我正在启动服务器。但是,我无法向他发送URL请求http://localhost:8080,但作为响应,控制台输出:错误:解析:找不到主机(权威) 代码基于以下内容编写:

//
//由Evgenii Grigorev于2020年11月8日创建。
//
#ifndef客户端\u客户端\u水电站
#定义客户端\u客户端\u水电站
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
命名空间beast=boost::beast;//从…起
命名空间http=beast::http;//从…起
名称空间net=boost::asio;//从…起
使用tcp=net::ip::tcp;//从…起
int客户端(int-argc,char*argv[]{
如果(argc!=5&&argc!=6){

std::cerr您可能将localhost:8080传递为主机。这将导致此处出现“主机未找到”错误:

auto const results=resolver.resolve(主机、端口)

因为解析程序中的resolve方法不应在主机参数中包含端口。请尝试将它们分开,如:./client localhost 8080等

//
// Created by Evgenii Grigorev on 08.11.2020.
//

#ifndef CLIENT_CLIENT_HPP
#define CLIENT_CLIENT_HPP
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <cstdlib>
#include <iostream>
#include <string>

namespace beast = boost::beast;  // from <boost/beast.hpp>
namespace http = beast::http;    // from <boost/beast/http.hpp>
namespace net = boost::asio;     // from <boost/asio.hpp>
using tcp = net::ip::tcp;        // from <boost/asio/ip/tcp.hpp>

int Client(int argc, char* argv[]) {
  if (argc != 5 && argc != 6) {
    std::cerr << "Usage: http-client-sync <host> <port> <target> "
                 "<request>[<HTTP version: 1.0 or 1.1(default)>]\n"
              << "Example:\n"
              << "    http-client-sync www.example.com 80 /v1/api/suggest {\"input\": \"<user_input>\"}\n"
              << "    http-client-sync www.example.com 80 / 1.0\n";
    return EXIT_FAILURE;
  }
  try {
    // Устанавливаем хост, порт, цель и запрос
    auto const host = argv[1];
    auto const port = argv[2];
    auto const target = argv[3];
    auto const request_body = argv[4];
    int version = argc == 5 && !std::strcmp("1.0", argv[5]) ? 10 : 11;
std::cout <<host << std::endl;
    // io_context требуется для всех операций ввода-вывода
    net::io_context ioc;

    // Эти объекты выполняют ввод-вывод
    tcp::resolver resolver{ioc};
    beast::tcp_stream stream(ioc);
    tcp::resolver::query query(host, port, boost::asio::ip::resolver_query_base::numeric_service);
    // Просмотр доменного имени
    auto const results = resolver.resolve(host, port);

    // Соединение по IP-адресу, полученному из поиска
    stream.connect(results);

    // Настройка сообщения HTTP POST-запроса
    http::string_body::value_type body = request_body;
    http::request<http::string_body> req{http::verb::post, target, version};
    req.set(http::field::host, host);
    req.set(http::field::body, body);
    req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
    req.set(http::field::content_type, "application/json");

    // Отправление HTTP-запроса на удаленный хост
    http::write(stream, req);

    // Этот буфер используется для чтения и должен быть сохранен
    boost::beast::flat_buffer buffer;

    // Объявление контейнера для хранения ответа
    http::response<http::string_body> res;

    // Получение HTTP-ответа
    http::read(stream, buffer, res);

    // Write the message to standard out
    std::cout << res << std::endl;

    // Корректное закрытие сокета
    beast::error_code ec;
    stream.socket().shutdown(tcp::socket::shutdown_both, ec);

    // иногда происходят not_connected (нет соединения)
    // так что не трудитесь сообщать об этом.
    //
    if(ec && ec != beast::errc::not_connected)
      throw beast::system_error{ec};

    // Здесь связь будет закрыта корректно
  } catch (std::exception const& e) {
    std::cerr << "Error: " << e.what() << std::endl;
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}
#endif  // CLIENT_CLIENT_HPP