0

I have asked the same question in bitcoin stackexchange. Here I am re-posting it to reach a wider audience.

I was going through this post in stack exchange How can I code a Bitcoin JSON-RPC “getwork” request in Java?

I tried to write a simple snippet for just the getwork json rpc.

public static void main(String[] args) throws Exception {

    String request = "{\"method\": \"getwork\", \"params\": [], \"id\":0}";
    URL url = new URL("http://de01.supportxmr.com:7777");

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    if (conn .getConnectTimeout() == 0)
        conn.setConnectTimeout(1000);
    if (conn.getReadTimeout() == 0)
        conn.setReadTimeout(1000);
    conn.setRequestMethod("POST");
    String encoded = Base64.getEncoder().encodeToString(("<my_wallet_addr>:x").getBytes(StandardCharsets.UTF_8));  //Java 8
    conn.setRequestProperty("Authorization", "Basic "+encoded);
    conn.setRequestProperty("Accept", "application/json");
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("Content-Length", Integer.toString(request.getBytes().length));
    conn.setRequestProperty("X-Mining-Extensions", "midstate");
    conn.setAllowUserInteraction(false);
    conn.setUseCaches(false);
    conn.setDoOutput(true);

    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(request);
    wr.close();

    InputStream is = conn.getInputStream();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int len;
    byte[] buffer = new byte[4096];
    while ((len = is.read(buffer)) != -1) {
        bos.write(buffer, 0, len);
    }
    String content = bos.toString();
    is.close();

    System.out.println(content);

}

when I run this code, I get an error

Exception in thread "main" java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:792)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:789)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1536)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at org.litecoinpool.miner.Test.main(Test.java:42)

What am I missing here? Is stratum proxy necessary to be running on the machine? If so how do I specify the parameters to run in the java code?

After changing the getwork to getblocktemplate / stratum as suggested, I tried a direct TCP connection also to the server.

public static void main(String[] args) throws Exception {
    String message1 = "{\"id\":1,\"method\":\"mining.subscribe\",\"params\":[]}";
    String authorizemessage = "{\"params\": [\"<wallet_address>\", \"x\"], \"id\": 2, \"method\": \"mining.authorize\"}";


    Socket soc = new Socket("de01.supportxmr.com", 7777);
    System.out.println("connected");
    OutputStream outputStream = soc.getOutputStream();
    outputStream.write(authorizemessage.getBytes());
    outputStream.flush();

    BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
    JSONObject json = new JSONObject(in.readLine());

    System.out.println("json response: " + json.toString());

    outputStream.write(message1.getBytes());
    outputStream.flush();

    in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
    json = new JSONObject(in.readLine());

    System.out.println("json response: " + json.toString());

}

but no luck again :(

user1223879
  • 123
  • 3
  • 13
  • 2
    Relevant: [Is cross-posting a question on multiple Stack Exchange sites permitted if the question is on-topic for each site?](https://meta.stackexchange.com/q/64068) – Bernhard Barker Jan 21 '18 at 18:37
  • Question appear to be removed from the other SO site. – ZF007 Jan 22 '18 at 00:20

0 Answers0