Android 到比特币节点的套接字连接
Android Socket connection to bitcoin node
我无法使用 android 上的简单 java 套接字从比特币节点获得响应。我发送版本消息并等待响应,但没有返回任何内容。代码相当简单:
private void connect(Peer peer) {
Log.i(App.TAG, "connect to: " + peer.ip + ":8333");
InetSocketAddress address = new InetSocketAddress(peer.ip, 8333);
Socket socket = new Socket();
try {
socket.connect(address, 10000);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
VersionMessage versionMessage = new VersionMessage();
writeMessage(versionMessage, out);
readMessage(in);
Log.i(App.TAG, "Shutting down....");
out.close();
in.close();
socket.close();
} catch (IOException e) {
Log.i(App.TAG, "Socket failed to conenct");
}
}
private void writeMessage(BaseMessage message, OutputStream out) {
Log.i(App.TAG, "writeMessage: " + message.getCommandName());
byte[] header = message.getHeader();
byte[] payload = message.getPayload();
try {
Log.i(App.TAG, "header: " + Util.bytesToHexString(header));
Log.i(App.TAG, "payload: " + Util.bytesToHexString(payload));
out.write(header);
out.write(payload);
} catch (IOException e) {
Log.i(App.TAG, "Failed to write message");
}
}
private void readMessage(InputStream in) throws IOException {
Log.i(App.TAG, "readMessage");
while (true) {
int b = in.read();
Log.i(App.TAG, "read: " + b);
if (b == -1) {
Log.i(App.TAG, "END OF CONNECTION!");
break;
}
response.add(b);
}
}
应用程序的日志如下:
connect to: 52.88.14.46:8333
writeMessage: version
header: f9beb4d976657273696f6e00000000006100000061000000
payload: 7c9c000001000000000000005a73788200000000524543495049454e54204950000000000000000000000000000053454e44455220495000000000000000000000000000000000006237646465346163626f657463686169000000000000000000
readMessage
read: -1
END OF CONNECTION!
Shutting down....
这里是一个link项目:https://github.com/boetchain/android-bitcoin-node
校验和字段是强制性的,其他节点将忽略没有有效校验和的传入消息。
我无法使用 android 上的简单 java 套接字从比特币节点获得响应。我发送版本消息并等待响应,但没有返回任何内容。代码相当简单:
private void connect(Peer peer) {
Log.i(App.TAG, "connect to: " + peer.ip + ":8333");
InetSocketAddress address = new InetSocketAddress(peer.ip, 8333);
Socket socket = new Socket();
try {
socket.connect(address, 10000);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
VersionMessage versionMessage = new VersionMessage();
writeMessage(versionMessage, out);
readMessage(in);
Log.i(App.TAG, "Shutting down....");
out.close();
in.close();
socket.close();
} catch (IOException e) {
Log.i(App.TAG, "Socket failed to conenct");
}
}
private void writeMessage(BaseMessage message, OutputStream out) {
Log.i(App.TAG, "writeMessage: " + message.getCommandName());
byte[] header = message.getHeader();
byte[] payload = message.getPayload();
try {
Log.i(App.TAG, "header: " + Util.bytesToHexString(header));
Log.i(App.TAG, "payload: " + Util.bytesToHexString(payload));
out.write(header);
out.write(payload);
} catch (IOException e) {
Log.i(App.TAG, "Failed to write message");
}
}
private void readMessage(InputStream in) throws IOException {
Log.i(App.TAG, "readMessage");
while (true) {
int b = in.read();
Log.i(App.TAG, "read: " + b);
if (b == -1) {
Log.i(App.TAG, "END OF CONNECTION!");
break;
}
response.add(b);
}
}
应用程序的日志如下:
connect to: 52.88.14.46:8333
writeMessage: version
header: f9beb4d976657273696f6e00000000006100000061000000
payload: 7c9c000001000000000000005a73788200000000524543495049454e54204950000000000000000000000000000053454e44455220495000000000000000000000000000000000006237646465346163626f657463686169000000000000000000
readMessage
read: -1
END OF CONNECTION!
Shutting down....
这里是一个link项目:https://github.com/boetchain/android-bitcoin-node
校验和字段是强制性的,其他节点将忽略没有有效校验和的传入消息。