$ bitcoin-cli help getconnectioncount
getconnectioncount

返回连接到其他节点的连接数。

结果:
n(整型)连接数

例子:
> bitcoin-cli getconnectioncount
> curl --user myusername:mypassword --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getconnectioncount", "params": [] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/

源码剖析

getconnectioncount 对应的函数在文件 rpcserver.h 中被引用。

extern UniValue getconnectioncount(const UniValue& params, bool fHelp);

实现在文件 rpcnet.cpp 中。

UniValue getconnectioncount(const UniValue& params, bool fHelp)
{
    if (fHelp || params.size() != 0)
        throw runtime_error(
            "getconnectioncount\n"
            "\nReturns the number of connections to other nodes.\n"
            "\nResult:\n"
            "n          (numeric) The connection count\n"
            "\nExamples:\n"
            + HelpExampleCli("getconnectioncount", "")
            + HelpExampleRpc("getconnectioncount", "")
        ); // 1. 帮助内容

    LOCK2(cs_main, cs_vNodes);

    return (int)vNodes.size(); // 2. 获取已建立连接的节点列表的大小并返回
}

1. 帮助内容

参考比特币 RPC 命令「getbestblockhash」1. 帮助内容

2. 获取已建立连接的节点列表的大小并返回

已建立连接的节点列表 vNodes 在文件 net.h 中被引用。

extern std::vector<CNode*> vNodes;

定义在文件 net.cpp 中。

vector<CNode*> vNodes;

参考链接