简体   繁体   English

如何修复Boost / Asio C ++异常错误?

[英]How do I fix the boost/asio c++ exception error?

I am trying to connect to the localhost port and I am getting the following error, how can I fix this error ? 我正在尝试连接到localhost端口,并且遇到以下错误,如何解决此错误?

" terminate called after throwing an instance of 'boost::exception_detail::clone_impl >' what(): connect: Connection refused " “抛出'boost :: exception_detail :: clone_impl>'what()实例后终止调用:what:连接:连接被拒绝“

#include <boost/asio.hpp>
#include <iostream>

int main() {
boost::system::error_code ec;
using namespace boost::asio;

io_service svc;
ip::tcp::socket sock(svc);
sock.connect({ {}, 3000 }); // localhost port

std::string response;

do {
    char buf[2048];
    size_t bytes_transferred = sock.receive(buffer(buf), {}, ec);
    if (!ec) response.append(buf, buf + bytes_transferred);
} while (!ec);

// print and exit
std::cout << response <<std::endl;
}

You can't connect to a port with nothing listening to it. 您无法一无所获地连接到端口。 That's what Connection Refused means: "nothing is here to respond to your request." 这就是Connection Refused的意思:“这里没有任何内容可以响应您的请求。”

You will have to run or use some other server for a connect() to succeed. 您必须运行或使用其他服务器才能使connect()成功。

If you're using a unix like system, you can probably use a tool like socat to quickly throw together a service to listen on a port that you can then connect to. 如果您使用的是类似Unix的系统,则可以使用socat之类的工具来快速组合服务,以监听随后可以连接到的端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM