Как скачать все части ТоррентаJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Как скачать все части Торрента

Сообщение Anonymous »

Я пытаюсь реализовать клиент BitTorrent и реализовал множество функций, включая анализ файла, подключение к узлам и чтение/отправку информации из сокета, но теперь я не понимаю, как все это объединить.

Код: Выделить всё

public void send_handshake() throws IOException {

byte[] l = {(byte) 19}, name = "BitTorrent protocol".getBytes(), reserved_bytes = new byte[8],
info_hash = torrent.info_hash_as_binary, peer_id = "12345678900987654321".getBytes();

byte[] c = combine(l,name,reserved_bytes,info_hash,peer_id);
out.write(c);

System.out.println("Sent Handshake");

}

Код: Выделить всё

public void send_request() throws Exception {

//variables initialized

for (int i = 0; i < numofblocks ; i++) {

bytes_offset = ByteBuffer.allocate(4).putInt(i*(int)Math.pow(2,14)).array();
block_length = ByteBuffer.allocate(4).putInt((int)Math.pow(2,14)).array();

if(lastblocklen!=0 && i==numofblocks-1){
block_length = ByteBuffer.allocate(4).putInt((int)lastblocklen).array();
System.out.println(Main.ANSI_BLUE + "Last block length is  :  " + new BigInteger(block_length).intValueExact() + " its size : "+ block_length.length + Main.ANSI_RESET);
}

out.write(SendRequest); // Sendrequest = all the byte array fields combined

System.out.println("Thread : "+ thread_num +" : " + "////// Sent request Message for block " + i + " of piece " + next_piece +" //////");

}
}

Код: Выделить всё

 public int read_message() throws Exception {

boolean handshake = false;
byte[] len = new byte[4];

int offset=0;
while(offset0)offset += t;
if(t==-1) return -1; //EOS error
}
if((int)len[0]==19 && (byte)len[1]=='B') handshake = true;

int length_of_message = new BigInteger(len).intValue();

byte[] messageid = new byte[1];
int rep = in.read(messageid,0,1);
int message_id = new BigInteger(messageid).intValueExact();

if(handshake){
// READ HANDSHAKE MESSAGE
}
else if (message_id == 5) {
try {
read_Bitfield(length_of_message);
return 0;
}
catch (Exception e){
throw new Exception();
}
}
else if (message_id == 4) {
read_Have(length_of_message);

}
else if (message_id == 1) {
//UNCHOKE MESSAGE

return 1; //unchoke

}
else if(message_id == 0){
//CHOKE MESSAGE
return 3;
}
else if(message_id == 7){
read_piece(length_of_message);
}
else if(message_id == 8){
//READ CANCEL MESSAGE
}
else {
System.out.println("message id not matched - message id : " + message_id);
throw new Exception();
}
return 10;
}

Код: Выделить всё

public int read_piece(int length_of_message) throws Exception {
//PIECE MESSAGE
//variables initialized
while(off 0) off += t1;
}
off = 0;
while(off 0) off += t1;
}
off = 0;
while(off 0) off += t1;
}
int index = new BigInteger(index_of_piece).intValueExact();

File f = new File(path + index); //path = location to my program directory

try (FileOutputStream fos = new FileOutputStream(path+index, true)) {
fos.write(data);
}
int bytes_sent_integer = new BigInteger(bytes_sent).intValueExact();
double blocknum = (bytes_sent_integer/Math.pow(2,14));

System.out.println("Thread : " + thread_num + " Written (Appended) block "  + blocknum + " to piece file " + index);

if(blocknum==numofblocks-1){
System.out.println("Thread : " + thread_num + " ////// We now have piece "+ index + " //////");
if(torrent.piece_hash_values_as_hex.contains(
Utils.bytesToHex(createSha1(new File(path+index))).toUpperCase())
) {
System.out.println("Thread : "+ thread_num +" : " + "CORRECTLY DOWNLOADED PIECE " + index);
synchronized (pieces){
pieces[index] = 1;
}
}
else{
System.out.println("Hash Mismatch...");

synchronized (pieces){
System.err.println("Piece value of " + index + " set to "+0);
pieces[index] = 0;
}
return -7;
}
}
return 0;
}
Я пытался создать два потока: один отправляет пакеты запросов подключенному узлу, а другой постоянно считывает входной поток из сокета для получения ответов, но это не удалось из-за неправильной синхронизации между потоками (при в конце загрузки поток чтения постоянно читает неправильные значения). Пожалуйста, предложите возможные способы реализовать это.

Подробнее здесь: https://stackoverflow.com/questions/785 ... -a-torrent
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»