Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;
  6. import java.io.OutputStreamWriter;
  7. import java.net.InetAddress;
  8. import java.net.Socket;
  9. import java.util.Scanner;
  10.  
  11. public class Client
  12. {
  13.  
  14. private static Socket socket;
  15.  
  16.  
  17. public static void main(String[] args)
  18. {
  19. try
  20. {
  21. int MAXVALUE = 10;
  22. int sleepTime = 1000;
  23. if (args.length < 4) {
  24. System.out.println("Use <number accounts> <sleepTime> <username> <password>");
  25. System.exit(0);
  26. }
  27. try {
  28. MAXVALUE = Integer.parseInt(args[0]);
  29. }
  30. catch(NumberFormatException e) {
  31. System.out.println(args[0]+" is not a valid integer...");
  32. System.out.println("Using 10 instead...");
  33. }
  34.  
  35. try {
  36. sleepTime = Integer.parseInt(args[1]);
  37. }
  38. catch(NumberFormatException e) {
  39. System.out.println(args[1]+" is not a valid integer...");
  40. System.out.println("Using 1000 instead...");
  41. }
  42.  
  43.  
  44. String username = args[2];
  45. String password = args[3];
  46.  
  47. System.out.println("OK: about to create "+MAXVALUE+" accounts for robottik.com. USERNAME: "+args[2]+", PASSWORD: "+args[3]);
  48. System.out.println("===PRESS RETURN TO BEGIN OR TYPE EXIT TO ABORT===");
  49. Scanner s = new Scanner(System.in);
  50. String userInput = s.nextLine();
  51. s.close();
  52. if (userInput.equalsIgnoreCase("exit")) {
  53. System.out.println("Goodbye.");
  54. System.exit(0);
  55. }
  56. System.out.println("---RUNNING PROGRAM---");
  57.  
  58. String host = "robottik.com";
  59. int port = 80;
  60. InetAddress address = InetAddress.getByName(host);
  61. socket = new Socket(address, port);
  62. System.out.println("Socket opened");
  63.  
  64. //Send the message to the server
  65. OutputStream os = socket.getOutputStream();
  66. OutputStreamWriter osw = new OutputStreamWriter(os);
  67. BufferedWriter bw = new BufferedWriter(osw);
  68. System.out.println("Request writer open...");
  69.  
  70. for (int i = 0; i<MAXVALUE; i++) {
  71.  
  72. String content = "username="+username+i+"&password="+password+"&confirm_password="+password;
  73. System.out.println(content);
  74.  
  75. //Create POST request
  76. String msg = "POST /register.php HTTP/1.1 \n" +
  77. "Host: robottik.com \n" +
  78. "Content-Type: application/x-www-form-urlencoded\n" +
  79. "Content-Length: "+content.length()+"\n" +
  80. "\n" +
  81. content;
  82.  
  83. String sendMessage = msg + "\n\n";
  84. bw.write(sendMessage);
  85. bw.flush();
  86. System.out.println("Message sent to the server... ("+i+")");
  87.  
  88. //Delete or comment this block of code to stop the client waiting for the server's response
  89. //Get the return message from the server
  90. InputStream is = socket.getInputStream();
  91. InputStreamReader isr = new InputStreamReader(is);
  92. BufferedReader br = new BufferedReader(isr);
  93. String message = br.readLine();
  94. System.out.println("Message received from the server :\n " +message);
  95. Thread.sleep(sleepTime);
  96. }
  97.  
  98.  
  99. }
  100. catch (Exception exception)
  101. {
  102. exception.printStackTrace();
  103. }
  104. finally
  105. {
  106. //Closing the socket
  107. try
  108. {
  109. socket.close();
  110.  
  111. }
  112. catch(Exception e)
  113. {
  114. System.out.println("Could not close socket");
  115. }
  116. }
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement