libbitcoin / libbitcoin-system

Bitcoin Cross-Platform C++ Development Toolkit
https://libbitcoin.info/
Other
1.3k stars 381 forks source link

Example code not working #1506

Closed awhooooo closed 2 months ago

awhooooo commented 2 months ago

Libbitcoin is a great project and I enjoy using it. The thing is, the following sample code I tried doesn't work and returns the "connection timed out" message. I tried other DNS seed servers and my own bitcoin node (tcp://localhost:8333) and the same issue still lingers. Are there any other examples I can use to successfully connect to a remote bitcoin node?

#include <bitcoin/system.hpp>
#include <bitcoin/client.hpp>
#include <string.h>
#include <iostream>
#include <cstdint>

using namespace bc;

int main()
{
    client::connection_type connection = {};
    connection.retries = 5;
    connection.timeout_seconds = 100;
    connection.server = config::endpoint("tcp://mainnet3.libbitcoin.net:9091");

    const auto on_reply = [](size_t blockHeight) 
    {
        std::cout << "Height: " << blockHeight << std::endl;
    };

    static const auto on_error = [](const code& ec) 
    {
        std::cout << "Error Code: " << ec.message() << std::endl;
    };

    client::obelisk_client client(connection);

    if(!client.connect(connection))
    {
        std::cout << "Fail" << std::endl;
    } else {
        std::cout << "Connection Succeeded" << std::endl;
    }

    client.blockchain_fetch_last_height(on_error, on_reply);
    client.wait();
}
evoskuil commented 2 months ago

Hello, this is client-server code. It operates only against a Libbitcoin v3 server, not the p2p protocol.

awhooooo commented 2 months ago

Thanks for the feedback.

thecodefactory commented 2 months ago

Try changing tcp://mainnet3.libbitcoin.net:9091 to tcp://mainnet1.libbitcoin.net:9091 and running it again