Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.util.Scanner;
- public class Client
- {
- private static Socket socket;
- public static void main(String[] args)
- {
- try
- {
- int MAXVALUE = 10;
- int sleepTime = 1000;
- if (args.length < 4) {
- System.out.println("Use <number accounts> <sleepTime> <username> <password>");
- System.exit(0);
- }
- try {
- MAXVALUE = Integer.parseInt(args[0]);
- }
- catch(NumberFormatException e) {
- System.out.println(args[0]+" is not a valid integer...");
- System.out.println("Using 10 instead...");
- }
- try {
- sleepTime = Integer.parseInt(args[1]);
- }
- catch(NumberFormatException e) {
- System.out.println(args[1]+" is not a valid integer...");
- System.out.println("Using 1000 instead...");
- }
- String username = args[2];
- String password = args[3];
- System.out.println("OK: about to create "+MAXVALUE+" accounts for robottik.com. USERNAME: "+args[2]+", PASSWORD: "+args[3]);
- System.out.println("===PRESS RETURN TO BEGIN OR TYPE EXIT TO ABORT===");
- Scanner s = new Scanner(System.in);
- String userInput = s.nextLine();
- s.close();
- if (userInput.equalsIgnoreCase("exit")) {
- System.out.println("Goodbye.");
- System.exit(0);
- }
- System.out.println("---RUNNING PROGRAM---");
- String host = "robottik.com";
- int port = 80;
- InetAddress address = InetAddress.getByName(host);
- socket = new Socket(address, port);
- System.out.println("Socket opened");
- //Send the message to the server
- OutputStream os = socket.getOutputStream();
- OutputStreamWriter osw = new OutputStreamWriter(os);
- BufferedWriter bw = new BufferedWriter(osw);
- System.out.println("Request writer open...");
- for (int i = 0; i<MAXVALUE; i++) {
- String content = "username="+username+i+"&password="+password+"&confirm_password="+password;
- System.out.println(content);
- //Create POST request
- String msg = "POST /register.php HTTP/1.1 \n" +
- "Host: robottik.com \n" +
- "Content-Type: application/x-www-form-urlencoded\n" +
- "Content-Length: "+content.length()+"\n" +
- "\n" +
- content;
- String sendMessage = msg + "\n\n";
- bw.write(sendMessage);
- bw.flush();
- System.out.println("Message sent to the server... ("+i+")");
- //Delete or comment this block of code to stop the client waiting for the server's response
- //Get the return message from the server
- InputStream is = socket.getInputStream();
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- String message = br.readLine();
- System.out.println("Message received from the server :\n " +message);
- Thread.sleep(sleepTime);
- }
- }
- catch (Exception exception)
- {
- exception.printStackTrace();
- }
- finally
- {
- //Closing the socket
- try
- {
- socket.close();
- }
- catch(Exception e)
- {
- System.out.println("Could not close socket");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement