Android Socket连接到比特币节点

时间:2018-02-01 20:33:20

标签: java android sockets bitcoin

我无法在Android上使用简单的java套接字从比特币节点获得响应。我发送了一个版本消息并等待响应,但没有返回任何内容。代码很简单:

<div id="player"></div>

<script src="http://www.youtube.com/player_api"></script>

<script>

    // create youtube player
    var player;
    function onYouTubePlayerAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: '0Bmhjf0rKe8',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
    }

    // autoplay video
    function onPlayerReady(event) {
        event.target.playVideo();
    }

    // when video ends
    function onPlayerStateChange(event) {        
        if(event.data === 0) {            
            player.loadVideoById('iRXJXaLV0n4');
        }
    }

</script>

应用程序的日志如下:

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);

    }
}

以下是该项目的链接:https://github.com/boetchain/android-bitcoin-node

1 个答案:

答案 0 :(得分:0)

checkSum字段是必需的,并且其他节点将忽略没有有效校验和的传入消息。