• 大小: 40KB
    文件类型: .java
    金币: 1
    下载: 0 次
    发布日期: 2021-06-09
  • 语言: Java
  • 标签: java  DHT爬虫  

资源简介

本来想找个java版的DHT爬虫玩玩 但是找了半天发现大部分都是nodejs和python的 github上找了几个要么运行有问题 要么代码包太大 于是自己写了个 爬了一天40w种子 不过去重后只有4w 分享一下源码 运行需要修改几个初始参数 同时需要登录路由器把本机的端口映射到外网 详情请看帖子 http://bbs.csdn.net/topics/391976465

资源截图

代码片段和文件信息

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.linkedBlockingQueue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main {
    
    public static void main(String[] args) throws Exception {
        // 填写外网ip 百度一下ip就能看到了
        final byte[] ip = {112 (byte) 65 (byte) 142 74};
        // 如果想收到其他节点发送过来的消息必须把外网端口映射到内网端口 这里是内网端口 只要登录路由器做一个端口映射就好
        final int port = 50000;
        // 节点数 这里表示从50000到50255这256个端口将会被监听
        final int nodeCount = 256;
        final int processorCount = Runtime.getRuntime().availableProcessors();
        final String id = “javadht:541241544“;
        
        List ips = new ArrayList<>();
        List ports = new ArrayList<>();
        List ids = new ArrayList<>();
        for(int i = 0; i < nodeCount; i++) {
            if(i % (nodeCount / processorCount) == 0 && ips.size() != 0) {
                new DHTServer(ips ports ids);
                ips = new ArrayList<>();
                ports = new ArrayList<>();
                ids = new ArrayList<>();
            }
            ips.add(ip);
            ports.add(port + i);
            ids.add((i + 740) + id);
        }
        if(ips.size() != 0) {
            new DHTServer(ips ports ids);
        }
    }
}

interface BencodeType {
    int getLength();
    int getTotalLength();
    byte[] getData();
    byte[] getTotalData();
}

class BencodeString implements BencodeType Comparable {
    
    private final String content;
    
    public BencodeString(String content) {
        this.content = content;
    }
    
    public BencodeString(byte[] bs) {
        try {
            this.content = new String(bs “iso-8859-1“);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
    
    public static BencodeString getString(String source int index) {
        char c = source.charAt(index);
        if(c >= ‘1‘ && c <= ‘9‘) {
            source = source.substring(index);
    

评论

共有 条评论