比特币矿池挖矿的地层连接
Stratum connection for bitcoin pool mining
我正在编写一个比特币矿工,它使用 stratum 协议在矿池中开采(参见文档 here。
层协议使用 JSON-RPC 2.0 作为编码,根据 JSON-RPC 2.0 规范(规范 here)我应该使用套接字创建连接游泳池。
我的问题是我似乎无法从池中收到响应。 JSON-RPC 2.0 声明对于我发送的每个请求对象,我都必须收到响应。
这是我的代码:
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\":[]}";
好的,伙计们..这是完整的运行代码。享受。
import java.io.PrintWriter;
import java.net.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class StratumSubscribe {
public static void main(String[] args)
{
connectToPool("sha256.hk.nicehash.com", 3334);
}
static void connectToPool(String host, int port)
{
try
{
InetAddress address = InetAddress.getByName(host);
System.out.println("Atempting to connect to " + address.toString() + " on port " + port + ".");
Socket socket = new Socket(address, port);
String message1 = "{\"id\":1,\"method\":\"mining.subscribe\",\"params\":[]}";
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output.println((message1));
System.out.println(input.readLine());//Hangs here.
}
catch (IOException e)
{
System.out.println(e.getMessage());
System.out.println("Error. Can't connect to Pool.");
System.exit(-2);
}
}
}
我正在编写一个比特币矿工,它使用 stratum 协议在矿池中开采(参见文档 here。
层协议使用 JSON-RPC 2.0 作为编码,根据 JSON-RPC 2.0 规范(规范 here)我应该使用套接字创建连接游泳池。
我的问题是我似乎无法从池中收到响应。 JSON-RPC 2.0 声明对于我发送的每个请求对象,我都必须收到响应。
这是我的代码:
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\":[]}";
好的,伙计们..这是完整的运行代码。享受。
import java.io.PrintWriter;
import java.net.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class StratumSubscribe {
public static void main(String[] args)
{
connectToPool("sha256.hk.nicehash.com", 3334);
}
static void connectToPool(String host, int port)
{
try
{
InetAddress address = InetAddress.getByName(host);
System.out.println("Atempting to connect to " + address.toString() + " on port " + port + ".");
Socket socket = new Socket(address, port);
String message1 = "{\"id\":1,\"method\":\"mining.subscribe\",\"params\":[]}";
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output.println((message1));
System.out.println(input.readLine());//Hangs here.
}
catch (IOException e)
{
System.out.println(e.getMessage());
System.out.println("Error. Can't connect to Pool.");
System.exit(-2);
}
}
}