user9661490
user9661490

Reputation:

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 ?

" terminate called after throwing an instance of 'boost::exception_detail::clone_impl >' what(): connect: Connection refused "

#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;
}

Upvotes: 0

Views: 669

Answers (1)

Max
Max

Reputation: 11000

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."

You will have to run or use some other server for a connect() to succeed.

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.

Upvotes: 1

Related Questions