java – 比特币池挖掘的Stratum连接

我正在编写一个使用stratum协议在池中挖矿的比特币矿工(参见文档 here.

stratum协议使用JSON-RPC 2.0作为编码,根据JSON-RPC 2.0规范(规范here),我应该使用套接字创建与池的连接.

我的问题是我似乎无法从池中收到回复. JSON-RPC 2.0声明对于我发送的每个Request对象,我必须收到回复.

这是我的代码:

public static void main(String[] args) 
{
    connectToPool("stratum.slushpool.com", 3333);
}    
static void connectToPool(String host, int port)
{
    try
    {
        InetAddress address = InetAddress.getByName(host);
        out.println("Atempting to connect to " + address.toString() + " on port " + port + ".");

        socket = new Socket(address, port);
        String message1 = "{\"jsonrpc\" : \"2.0\", \"id\": 1, \"method\": \"mining.subscribe\", \"params\": []}";

        PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        output.write((message1 + "\\n"));
        out.println(input.readLine());//Hangs here.
    }
    catch (IOException e) 
    {
        out.println(e.getMessage());
        out.println("Error. Can't connect to Pool.");
        System.exit(-2);
    }
}

最佳答案 经过数小时的修修补补,我找到了解决方案.

显然JSON字符串不应该有任何空格.所以代替:

String message1 = "{\"jsonrpc\" : \"2.0\", \"id\": 1, \"method\": \"mining.subscribe\", \"params\": []}";

它应该是:

String message1 = "{\"id\":1,\"method\":\"mining.subscribe\",\"params\":[]}";
点赞