Android Socket connection to bitcoin node

0 votes

I am using the following code to get response from bitcoin node. But when I execute it, I get no response.

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

    }
}

The logs from the app are as follows:

connect to: 52.88.14.46:8333
writeMessage: version
header: f9beb4d976657273696f6e00000000006100000061000000
payload: 7c9c000001000000000000005a73788200000000524543495049454e54204950000000000000000000000000000053454e44455220495000000000000000000000000000000000006237646465346163626f657463686169000000000000000000
readMessage
read: -1
END OF CONNECTION!
Shutting down....
Aug 29, 2018 in Blockchain by digger
• 26,740 points

retagged Nov 22, 2018 by Kalgi 614 views

1 answer to this question.

0 votes

The checkSum field is mandatory and fellow nodes will just ignore incoming messages without a valid checksum.

answered Aug 29, 2018 by slayer
• 29,350 points

Related Questions In Blockchain

+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
960 views
0 votes
1 answer

Connection failed to Ethereum Ropsten Testnet network.

 It looks like your node isn't syncing ...READ MORE

answered Mar 27, 2018 in Blockchain by Christine
• 15,790 points
1,494 views
+1 vote
1 answer
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,450 points
1,142 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,690 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,232 views
0 votes
1 answer

AWS multichain network, "Couldn't connect to the seed node" error

The error says "please check multichaind is ...READ MORE

answered Jul 9, 2018 in Blockchain by slayer
• 29,350 points
1,227 views
0 votes
1 answer

How to add Bitcoin as currency for my website?

You cannot use BTC as the currency ...READ MORE

answered Aug 17, 2018 in Blockchain by slayer
• 29,350 points
831 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP