更新#4:为使用UDP&而添加的Demo java片段发送宣告消息(记住连接是第一个!)检查自己的响应。
=============================================== =====
更新#3:我设法让它工作,方法doConnect()呈现 贝娄是好的,我自己的回复中有更多信息。
=============================================== =====
我主要对如何在发布url的协议是UDP时下载跟踪器响应感兴趣。
详细说明: 所以这些是来自有效的torrent文件的一些公告URL(第一个是主要文件)
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
如果protocol是UDP,则URL构造函数抛出“java.net.MalformedURLException:unknown protocol:udp”
所以我想问题可以恢复到以下问题:如何从UDP协议上的URL下载恢复? (希望它简单,我看不到数据报的东西)
更新#1:
我在网上做了更多的调查&到达下面粘贴的结构 (应该工作..但不是,我的意思是在本地,但不是真正的跟踪器)
指向规范的链接:http://www.bittorrent.org/beps/bep_0015.html
Ex:这是我创建套接字的方式,但在有效的跟踪器上,我从来没有收到任何回复作为响应,所以有些工作:
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;
}
问题仍然存在..我从未收到过跟踪器的响应(也尝试过其他网址) 新问题:在elbitz.net上创建套接字是否可以,端口:80,当完整网址包含更多信息时(例如:/ announce)?
更新#2
上面的代码似乎工作正常..我在google上找到了一个已实现此规范的跟踪器列表&amp;发生了回应(例如:“udp://tracker.openbittorrent.com:80”)
新问题&amp;再次规范在这里:http://www.bittorrent.org/beps/bep_0015.html - 我似乎没有看到如何获得同行名单? ..在对洪流跟踪器的正常请求中(通过http),有2种情况:正常响应(bencoded map)&amp;压缩响应(二进制形式)。现在是同行的名单了吗?
/* 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地址&amp; TCP端口 ..而且每个请求我得到一个回复..所以这不可能!我需要一份同行名单!
请帮忙!我还没有实现的唯一类型的响应消息是scrape&amp;错误...但没有人包含我感兴趣的信息(同行信息:ip&amp; port) 例如:刮痧
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
答案 0 :(得分:3)
我对自己的问题的回答..非常感谢,我!
UPDATE ***
DatagramSocket workingSocket = ?;//
DatagramPacket sendPacket = null, receivePacket = null;
byte[] sendData = null;
byte[] receiveData = null;
receiveData = new byte[65508]; //NOTE: at least 16 bytes | 65508 is max size, unused bytes are 0
receivePacket = new DatagramPacket(receiveData, receiveData.length);
sendData = this.getRequestTypeAnnounce(announceUDPWrapper.a_TransactionId);
sendPacket = new DatagramPacket(sendData, sendData.length);
workingSocket.send(sendPacket);
workingSocket.receive(receivePacket);
byte[] fullResponse = receivePacket.getData();
private byte[] getRequestTypeAnnounce(AnnounceUDPWrapper announceUDPWrapper) {
//long connectionId, int transactionId, int tcpPort
ByteBuffer bBuffer = ByteBuffer.allocate(98);
bBuffer.putLong(announceUDPWrapper.a_ConnectionId);
bBuffer.putInt(ACTION_ID_ANNOUNCE);
bBuffer.putInt(announceUDPWrapper.a_TransactionId);
bBuffer.put(announceUDPWrapper.a_InfoHash);//<<<< what you asked.. adding the infoHash which is byte[]
bBuffer.put(this.m_MyId);
bBuffer.putLong(announceUDPWrapper.a_Downloaded);
bBuffer.putLong(announceUDPWrapper.a_Uploaded);
bBuffer.putLong(announceUDPWrapper.a_Left);
bBuffer.putInt(announceUDPWrapper.a_EventType.ordinal());
bBuffer.put(Utils.intToByteArray(0));// ip, 0 = default
bBuffer.putInt(0);//key
bBuffer.putInt(10);//num_want
byte[] portAsBytes = Utils.intToByteArray(announceUDPWrapper.a_ListeningTCPPort);
bBuffer.put(Utils.subArray(portAsBytes, 2, 2)); //port
return bBuffer.array();
}
答案 1 :(得分:0)
我觉得你在这里混合网络层。
udp位于较低层 - http://通过tcp,而其他一些协议可以通过udp。 IP低于此值,因此您尝试使用的协议类似于&#34; torrent over udp,over IP&#34;。
有关torrent / udp协议文档,请参阅here。