我使用套接字开发了比特币协议,但是遇到了一些问题。首先,当我的代码连接到比特币节点时,它没有给出任何响应。我发送了版本消息,但另一侧节点未重新发送任何消息。我认为问题可能出在我错误地实施了协议,但我检查并认为这很好。 (2)其次,我认为使用套接字连接到比特币节点可能是不可能的,但这是愚蠢的主意。是吗?
一件重要的事情,我只编码了套接字连接,将代码发送到https://bitnodes.earn.com/nodes/上的比特币节点,以及从节点接收的消息,但它始终响应-1。 还有一件事,我不给出节点响应,也不给出连接错误。 (我只是说)
请帮助我:)
我发送版本消息但又没有从节点发送另一个消息是什么问题?
此外,在尝试连接到套接字时,我还会附加控制台的输出(连接阶段和协议结构)。
Connected
f9beb4d976657273696f6e0000000000000000660a75c8397f1101000100000000000000820b905c00000000010000000000000000000000000000000000ffff340e5966208d010000000000000000000000000000000000ffff7f000001208db910e81a17a5dd79102f417572696d61733a302e31362e332f0000000001
Magic:f9beb4d9
Command:76657273696f6e0000000000
Length:00000066
Checksum:0a75c839
Payload:7f1101000100000000000000820b905c00000000010000000000000000000000000000000000ffff340e5966208d010000000000000000000000000000000000ffff7f000001208db910e81a17a5dd79102f417572696d61733a302e31362e332f0000000001
Version:7f110100
Services:0100000000000000
timestamp:820b905c00000000
addr_recv:010000000000000000000000000000000000ffff340e5966208d
addr_from:010000000000000000000000000000000000ffff7f000001208d
nonce:b910e81a17a5dd79
user_agent:102f417572696d61733a302e31362e332f
start_height:00000000
relay:01
Connection end
主类:
private static String address = "52.14.89.102";
private static int port = 8333;
public static void main(String [] args) throws IOException {
String line = "";
try {
socket = new Socket(address, port);
System.out.println("Connected");
input = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
VersionMessage message = new VersionMessage();
line = Hex.encodeHexString(message.getMessage());
System.out.println(line);
structure(message.getMessage());
out.writeBytes(line);;
} catch(UnknownHostException u) {
System.out.println(u);
} catch (IOException e) {
e.printStackTrace();
}
...
VersionMessage类的一部分:
public VersionMessage() {
super();
this.version = 70015;
this.services = 1; // NODE_NETWORK
this.timestamp = System.currentTimeMillis() / 1000;
this.nonce = new Random().nextLong();
this.userAgent = "/Satoshi:0.16.3/";
this.startHeight = 0;
this.relay = true;
contructPayload();
contructMessage();
}
@Override
public void contructPayload() {
addByteArray(dataParser.toInt32(version));
addByteArray(dataParser.toInt64(services));
addByteArray(dataParser.toInt64(timestamp));
addAddr("52.14.89.102", 8333);
addAddr("127.0.0.1", 8333);
addByteArray(dataParser.toInt64(nonce));
addStr(userAgent);
addByteArray(dataParser.toInt32(startHeight));
addByteArray(dataParser.toInt8(relay ? (byte) 1 : (byte) 0));
payload = new byte[payloadBytes.size()];
IntStream.range(0, payloadBytes.size()).forEach(i -> payload[i] = payloadBytes.get(i).byteValue());
}
BitcoinMessage(VersionMessage的父级):
public BitcoinMessage() {
magic = 0xf9beb4d9;;
payloadBytes = new ArrayList<Integer>();
dataParser = new DataParser();
}
public void contructMessage() {
message = new byte[MAGIC_LENGTH + COMMAND_LENGTH + PAYLOAD_LENGTH_SIZE + CHECKSUM_LENGTH + payload.length];
length = payload.length;
byte[] doubleHashedPayload = MessageUtils.doubleHashWithSha256(payload);
MessageUtils.addToByteArray(MessageUtils.intToBytes(magic, MAGIC_LENGTH), 0, MAGIC_LENGTH, message);
MessageUtils.addToByteArray(getCommandName().getBytes(), MAGIC_LENGTH, COMMAND_LENGTH, message);
MessageUtils.addToByteArray(MessageUtils.intToBytes(length, PAYLOAD_LENGTH_SIZE), MAGIC_LENGTH + COMMAND_LENGTH, PAYLOAD_LENGTH_SIZE, message);
MessageUtils.addToByteArray(doubleHashedPayload, MAGIC_LENGTH + COMMAND_LENGTH + PAYLOAD_LENGTH_SIZE, CHECKSUM_LENGTH, message);
MessageUtils.addToByteArray(payload, MAGIC_LENGTH + COMMAND_LENGTH + PAYLOAD_LENGTH_SIZE + CHECKSUM_LENGTH, payload.length, message);
}