Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: nakshathri on September 08, 2014, 11:09:17 AM



Title: Bitcoind : Java Client : Unexpected end of file from server Error
Post by: nakshathri on September 08, 2014, 11:09:17 AM
Hello All,

I am very new to bitcoin and this is my first experiment with bitcoind.

We have been trying to develop an Java based application on BTC using bitcoind. We are using simple HTTP Post using Jersey client with basic authentication like given below.


Client client = Client.create();

String url = "http://"+username+':'+password+"@localhost:18333";
//String url = "http://localhost:18333";
System.out.println("URL is : "+url);
WebResource webResource = client.resource(url);

Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
   return new PasswordAuthentication (username, password.toCharArray());
    }
    });

String input = "{\"method\":\"getblockcount\",\"params\":[],\"id\":\"1\"}";
ClientResponse response = webResource.type("application/json").post(ClientResponse.class, input);

When we execute this, we are getting

Caused by: java.net.SocketException: Unexpected end of file from server
   at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:772)
   at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
   at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:769)

From the exception what I understand is, there are some server side errors but i am not able to see errors in the log files.

The entries in the bitcoin.conf file is as follows:

rpcuser=bitcoinrpc
rpcpassword=5UKQTzWTf7EEJnkShZhr9tbjpDVNmLMgQkFfWsnZhLey
testnet=1

Really appreciate any help in resolving this error.  Thank you in advance.

regards,
Manjunath


Title: Re: Bitcoind : Java Client : Unexpected end of file from server Error
Post by: Newar on September 08, 2014, 04:47:13 PM
This may be silly question, but do you also have server=1 in your conf?


Title: Re: Bitcoind : Java Client : Unexpected end of file from server Error
Post by: nakshathri on September 09, 2014, 03:43:08 AM
Yes, i do have that configuration set. And also i am starting the server with following option:

bitcoind -server -listen

Let me know if you need any further details.

Regards,
Manjunath


Title: Re: Bitcoind : Java Client : Unexpected end of file from server Error
Post by: nakshathri on September 09, 2014, 05:48:36 AM
When I inspect the request and response, its giving "Remote server closed the connection before sending response header" error as part of HTTP failure scenario. Following is the request data content :

URL : http://192.168.2.111:18333/

Request Data:

{
   "method": "getblockcount",
   "params": [],
   "id": "1"
}

Please help me in understanding where the mistake is.

Regards,
Manjunath


Title: Re: Bitcoind : Java Client : Unexpected end of file from server Error
Post by: nakshathri on September 09, 2014, 06:08:19 AM
I added below entries to bitcoin.conf file. But still giving the same error:

rpcallowip=192.168.2.111
rpcallowip=127.0.0.1



Title: Re: Bitcoind : Java Client : Unexpected end of file from server Error
Post by: virtualx on September 09, 2014, 09:49:17 AM
I don't know what's wrong in this code, but there seem to be some working examples here:
https://github.com/cdelargy/BitcoinRPCClient/blob/master/src/main/java/com/cdelargy/bitcoin/RPCClient.java (https://github.com/cdelargy/BitcoinRPCClient/blob/master/src/main/java/com/cdelargy/bitcoin/RPCClient.java)
http://bitcoin.stackexchange.com/questions/7529/how-to-communicate-between-java-and-bitcoind (http://bitcoin.stackexchange.com/questions/7529/how-to-communicate-between-java-and-bitcoind)

Maybe it helps  :)


Title: Re: Bitcoind : Java Client : Unexpected end of file from server Error
Post by: nakshathri on September 09, 2014, 10:27:37 AM
Thank you. But after all the tweaking, i am able to get it working properly. For the benefit of others, here is the Java Code for making JSON-RPC calls to bitcoind (Using Jersey Client):

bitcoin.conf entries :

rpcuser=bitcoinrpc
rpcpassword=5UKQTzWTf7EEJnkShZhr9tbjpDVNmLMgQkFfWsnZhLey   
testnet=1
server=1
#txindex=1
rpcallowip=192.168.2.*
rpcallowip=127.0.0.1
rpcport=8999
#rpctimeout=60000

Make sure you change the port number and dont forget to provide rpcallowip entry pointing to respective IP address.

Client Code:

DefaultClientConfig config = new DefaultClientConfig();

config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
            Boolean.TRUE);
Client client = Client.create(config);
client.addFilter(new HTTPBasicAuthFilter(username, password));
WebResource webResource = client.resource(url);
String input = "{\"id\":\"jsonrpc\",\"method\":\"listaccounts\",\"params\":[]}";
ClientResponse response = webResource.accept("application/json").type("application/json")
           .post(ClientResponse.class, input);

Thats it. Your good to start with bitcoind integration.

Regards,
Manjunath