资源简介
本来想找个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);
相关资源
- java DHT爬虫 重构并加了完整的注释 修
- JavaEE项目案例源码
- Java开发的游戏 贪吃蛇 完整源代码
- JAVA简版图书馆管理程序
- JAVA VOX转WAR转换器
- Java安全通信
- 小型图书管理系统jsp+javabean+servlet+s
- 动物识别系统java版、C版
- Utils.java
- java时钟_表盘数字
- Java开发学生管理系统-project
- jconsole.1.8.0jar
- Delphi10.3 中通过JNI调用 Java 函数
- msmqjava.dll 64位版本 测试可用
- Java游戏\\猫和老鼠.jar
- Java游戏\\埃及祖玛.jar
- JavaFx WeChat聊天软件服务端
- Java在线编辑编译运行
- 助学贷款管理系统纯JAVA语言编写,数
- 泡泡龙游戏(JAVA写的)
- MODEL 2 的登录功能实现 servlet+jsp+java
- 达内java培训内部资料pdf 百度网盘(有
- Java实现的打字游戏
- Java实现的五子棋
- 遗传算法 tsp java
- Java+Mysql课程设计 学生成绩管理系统连
- JAVA编程题全集100题及答案——直接打
- CMPP2.0JAVA调用
- 员工工资管理系统java+sql
- java考勤数据通勤系统
评论
共有 条评论