更新#4:添加了用于处理UDP并发送宣布msg(请记住先连接!)的演示Java代码段。请检查下面的响应。
================================================ ====
更新#3:我设法使其工作,并提供了方法doConnect()波纹管很好,请在我自己的波纹管中获取更多信息。
================================================ ====
我主要是在公告网址的协议为UDP时如何下载跟踪器响应。
详细信息:因此,这些是有效洪流文件中的一些宣告网址(第一个是主要网址)
http://tracker.torrentbox.com:2710/announce
udp://elbitz.net:80/announce.php?passkey=362fc69de3402e8ef5794c7ecf7c58d4
udp://tracker.podtropolis.com:2711/announce
如果协议是HTTP,那么一切都会顺利进行,这就是我的工作方式:
String fullUrl = announceURL + "?info_hash=" + this.m_TorrentInfoHashAsURL + .. // i add the params
URL url = new URL(fullUrl);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
.. //reading the stream
如果协议是UDP,则URL构造函数将引发“ java.net.MalformedURLException:未知协议:udp”
因此,我想问题可以恢复为以下几点:如何从UDP协议上的URL下载断言?(希望它简单,我看不到数据报的内容)
UPDATE#1:
我在线进行了更多调查,得出了下面粘贴的以下结构(应该工作..但是不行,我的意思是本地可以,但是不能使用真正的跟踪器)
链接到规格:http://www.bittorrent.org/beps/bep_0015.html
例如:这是我创建套接字的方式,但是在有效的跟踪器上,我从没有收到任何响应,因此无法正常工作:
if full url: udp://elbitz.net:80/announce.php?passkey=362fc69de3402e8ef5794c7ecf7c58d4
this.m_TrackerHost: elbitz.net
this.m_TrackerPort: 80
private DatagramSocket m_WorkingSocket;
private DatagramSocket getWorkingSocket() {
Logger.d(TAG, "getWorkingSocket()");
if(this.m_WorkingSocket==null){
Random rnd = new Random();
for (int i = 0; i < 100; i++) {
try {
int port = 15000 + rnd.nextInt(15000); // [15000-30000)
DatagramSocket result = new DatagramSocket(port);
InetAddress trackerAddress = InetAddress.getByName(this.m_TrackerHost);
result.connect(trackerAddress, this.m_TrackerPort);
this.m_WorkingSocket = result;
} catch (SocketException se) {
Logger.w(TAG, "getWorkingSocket() - port is taken");
} catch (SecurityException se) {
Logger.w(TAG, "getWorkingSocket() - port is blocked?");
} catch (UnknownHostException e) {
Logger.w(TAG, "getWorkingSocket() - unkwnown host?");
}
}
}
return this.m_WorkingSocket;
}
&现在来自doConnect的完整代码应该是第一个通信阶段(接下来将宣布..此处类似的代码)
private boolean doConnect() throws IOException{
Logger.d(TAG, "doConnect()");
DatagramSocket workingSocket = this.getWorkingSocket();
DatagramPacket sendPacket = null, receivePacket = null;
byte[] sendData = null;
byte[] receiveData = null;
int round = 0;
Logger.d(TAG, "doConnect(): first round, timeout: " + this.getTimeoutInMillis(ACTION_ID_CONNECT, round));
while(true) {
if(round==8){
Logger.w(TAG, "doConnect() - failed to connect with tracker, consumed in vain all 8 rounds..");
return false;
}
workingSocket.setSoTimeout(this.getTimeoutInMillis(ACTION_ID_CONNECT, round));
if(receivePacket==null){
/*
Offset Size Name Value
0 32-bit integer action 0 // connect
4 32-bit integer transaction_id
8 64-bit integer connection_id
16 */
receiveData = new byte[16]; //CONNECT: at least 16 bytes
receivePacket = new DatagramPacket(receiveData, receiveData.length);
sendData = this.getConnectRequest();//return byte[] with everything..just like in specs
sendPacket = new DatagramPacket(sendData, sendData.length);
}
try {
Logger.d(TAG, "doConnect() - sending connect data: " + (Arrays.toString(sendData)));
workingSocket.send(sendPacket);
workingSocket.receive(receivePacket);
break;
} catch (SocketTimeoutException ste) {
round ++;
Logger.w(TAG, "doConnect() connect - new round: " + (round+1) + "|timeout: " + this.getTimeoutInMillis(ACTION_ID_CONNECT, round));
continue;
}
}
byte[] connectResponse = receivePacket.getData();
int actionIdFromResponse = Utils.byteArrayToInt(Utils.subArray(connectResponse, 0, 4));
int transactionIdFromResponse = Utils.byteArrayToInt(Utils.subArray(connectResponse, 4, 4));
long connectionIdFromResponse = Utils.byteArrayToLong(Utils.subArray(connectResponse, 8, 8));
if(transactionIdFromResponse!=this.m_TransactionId){
Logger.w(TAG, "doConnect() - received different transactionId");
return false;
}
if(actionIdFromResponse!=ACTION_ID_CONNECT){
Logger.w(TAG, "doConnect() - didnt received ACTION_ID_CONNECT");
return false;
}
//store connectionId
this.m_ConnectionId = connectionIdFromResponse;
return true;
}
问题仍然存在。我也从未收到跟踪器的回复(也尝试过其他网址)新问题:当完整的URL包含更多信息(例如:/ announce)时,可以在elbitz.net上创建套接字,端口:80吗?
更新#2
上面的代码似乎可以正常工作。我在Google上找到了已实施此规范的跟踪器列表,并且发生了响应(例如:“ udp://tracker.openbittorrent.com:80”)]
新问题,再次在此处指定规格:http://www.bittorrent.org/beps/bep_0015.html-我似乎看不到如何获得同行名单?..在对torrent跟踪程序的正常请求(通过http)中,有2种情况:正常响应(一个bencoded映射)和压缩响应(二进制形式)。那现在是同行名单了吗?
/* Offset Size Name Value 0 32-bit integer action 1 // announce 4 32-bit integer transaction_id 8 32-bit integer interval 12 32-bit integer leechers 16 32-bit integer seeders 20 + 6 * n 32-bit integer IP address 24 + 6 * n 16-bit integer TCP port 20 + 6 * N */
从我的测试中,我总是收到以下字段的相同值:IP地址和TCP端口..再加上我每个请求都会得到一个响应..so这个问题就解决了!..我需要一个同行列表!
请帮助!
我尚未实现的唯一响应消息类型是“刮擦和错误...”,但是没有人包含我感兴趣的信息(对等信息:ip和端口)例如:刮擦Offset Size Name Value
0 32-bit integer action 2 // scrape
4 32-bit integer transaction_id
8 + 12 * n 32-bit integer seeders
12 + 12 * n 32-bit integer completed
16 + 12 * n 32-bit integer leechers
8 + 12 * N
更新#4:添加了用于处理UDP并发送宣布msg(请记住先连接!)的演示Java代码段。请检查以下响应。 ================================================== == ...
我对自己问题的回答。非常感谢,我!
对我来说,您似乎在这里混合网络层。