使用boost asio写入碳会导致管道损坏

时间:2017-03-03 12:43:13

标签: c++ boost boost-asio graphite

我在本地主机上运行Grafana和Graphite。一切都默认设置,因此明文协议配置为端口2003,如here所述

以下工作符合要求:

export SERVER=localhost
export PORT=2003
echo "no_cluster.fake_xen.sample 25 1488542618" | nc  ${SERVER} ${PORT}

给我我期望的数据点(根据需要调整时间戳)。

以下最小可编译示例:

#include <chrono>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <boost/asio.hpp>

namespace basio = boost::asio;

void post_to_carbon (std::string message)
{
    using btcp = boost::asio::ip::tcp;
    constexpr const char* carbon_port="2003";
    basio::io_service ios;
    btcp::resolver resolver(ios);
    btcp::resolver::query query("localhost", carbon_port);
    btcp::endpoint carbon_endpoint = *resolver.resolve(query);
    btcp::socket sock(ios,carbon_endpoint);
    boost::system::error_code ignored_error;
    basio::write(sock, basio::buffer(message), ignored_error);
    std::cout << "posting: " << message << " gave: " << ignored_error.message() << "\n";

}

int main() {
    post_to_carbon("no_cluster.fake_xen.sample 25 1488542800");
}

失败并显示错误消息:

posting: no_cluster.fake_xen.sample 25 1488542800 gave: Broken pipe

有人能告诉我我做错了吗?

1 个答案:

答案 0 :(得分:0)

构造函数形式@Override @SuppressWarnings("rawtypes") public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { if(testMethod.getAllCalledMethods.contains(".clickUserSettings()")){ annotation.setEnabled(false); } 将套接字的本地端点绑定到给定端点。

我认为你想要做的是:

socket(io_service, endpoint)