Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. static boolean connect2Pool(String SERVER, int PORT)
  2. {
  3. String message1 = "{"id":1,"method":"mining.subscribe","params":[]}";
  4. String authorizemessage = "{"params": ["" + USERNAME + "." + WORKER + "", "" + PASSWORD + ""], "id": 2, "method": "mining.authorize"}";
  5.  
  6. boolean result = false;
  7. DataInputStream is;
  8. DataOutputStream os;
  9.  
  10. try
  11. {
  12. InetAddress address = InetAddress.getByName(SERVER);
  13. System.out.println("Atempting to connect to " + address.toString() + " on port " + PORT + ".");
  14.  
  15.  
  16. // connect
  17.  
  18. Socket socket = new Socket();
  19. socket.connect(new InetSocketAddress(SERVER, PORT));
  20. is = new DataInputStream(socket.getInputStream());
  21. os = new DataOutputStream(socket.getOutputStream());
  22. PrintWriter pw = new PrintWriter(os);
  23. pw.println(message1); //connect
  24. pw.flush();
  25.  
  26. //read response
  27.  
  28. BufferedReader in = new BufferedReader(new InputStreamReader(is));
  29. JSONObject json = new JSONObject(in.readLine());
  30.  
  31. if(!json.has("result")) {
  32. System.out.println("no reult");
  33. result=false;
  34. }else {
  35. System.out.println("json response: " + json.toString());
  36. result=true;
  37. }
  38.  
  39. pw.println(authorizemessage); //authorize
  40. pw.flush();
  41.  
  42. //read response
  43.  
  44. in = new BufferedReader(new InputStreamReader(is));
  45. json = new JSONObject(in.readLine());
  46.  
  47. if(!json.has("result")) {
  48. System.out.println("no reult");
  49. result=false;
  50. }else {
  51. System.out.println("json response: " + json.toString());
  52. result=true;
  53. }
  54.  
  55.  
  56. is.close();
  57. os.close();
  58. socket.close();
  59.  
  60.  
  61. } catch (IOException e) {
  62. System.out.println(e.getMessage());
  63. System.out.println("Not able to connect to pool");
  64. System.exit(-2);
  65. } catch (JSONException e) {
  66. System.out.println(e.getMessage());
  67. System.out.println("JSON not good.");
  68. System.exit(-2);
  69. }
  70.  
  71.  
  72. return result;
  73.  
  74. } //end of connect2Pool
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement