Issue getting JSON-RPC response using Java client

+1 vote

So I am trying to write a Java client that can communicate to the multichain server. The server is running on an Ubuntu VM (VM1) and I am writing my code on another Ubuntu VM (VM2).

Both VMs are on the same network and can communicate with one another. I can run multichain-cli commands from VM2 and get a successful response.

i was following the example from http://www.multichain.com/qa/2331/help-connection-to-mutlichain-using-java-client and I have the following code;

 

public JSONObject invokeRPC(String id, String method, List<Object> params, String chainName){
    HttpClient httpClient = HttpClientBuilder.create().build();
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("id", id);
    jsonObject.put("method", method);
    if(params != null && params.size() != 0){
        jsonObject.put("params", params);
    }
    jsonObject.put("chain_name", chainName);
    JSONObject responseJSONObject = new JSONObject();
    try{
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                new AuthScope("192.168.1.6", 9732),
                new UsernamePasswordCredentials("multichainrpc", "EZikv3MtoKA2yjrG6T7eTkPZMXntwr9k1ft7ja3jLLaA")
        );
        StringEntity myEntity = new StringEntity(jsonObject.toString());

        HttpPost httpPost = new 
HttpPost("http://192.168.1.6:9732");       //HttpPost("http://multichainrpc:EZikv3MtoKA2yjrG6T7eTkPZMXntwr9k1ft7ja3jLLaA@192.168.1.6:9732");
        httpPost.setEntity(myEntity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

        System.out.println("------------------");
        System.out.println(httpResponse.getStatusLine());

        if(httpEntity != null){
            System.out.println("Response content length: " + httpEntity.getContentLength());
        }

        responseJSONObject = new JSONObject().getJSONObject(EntityUtils.toString(httpEntity));

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        httpClient.getConnectionManager().shutdown();
    }

    return responseJSONObject;
}
 

The code fails at 'HttpResponse httpResponse = httpClient.execute(httpPost);' with the following Exception;

org.apache.http.conn.HttpHostConnectException: Connect to 192.168.1.6:9732 [/192.168.1.6] failed: Connection refused (Connection refused)

at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158)

at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)

at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)

at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)

at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)

at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)

at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)

at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)

at com.recordz.general.connect.Connect.invokeRPC(Connect.java:52)

at com.recordz.general.connect.Connect.main(Connect.java:28)

Caused by: java.net.ConnectException: Connection refused (Connection refused)

at java.net.PlainSocketImpl.socketConnect(Native Method)

at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)

at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)

at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)

at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)

at java.net.Socket.connect(Socket.java:589)

at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)

at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)

... 12 more

 

I am not sure why this is happening as both VMs are on the same LAN and can communicate with one another.

Any advice would be much appreciated

asked Jul 8, 2017 by doldy101

1 Answer

+1 vote
So I found the solution.

On the client vm I had to edit the multichain.conf file in the blockchain directory with the rpcallowip entry.

I had previously done this, but on the blockchain vm VM1.
answered Jul 9, 2017 by doldy101
...