-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp2pPeerThread.java
More file actions
48 lines (40 loc) · 1.13 KB
/
p2pPeerThread.java
File metadata and controls
48 lines (40 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.io.*;
import java.net.*;
import java.util.*;
public class p2pPeerThread extends Thread {
protected DatagramSocket socket = null;
protected DatagramPacket pacote = null;
protected InetAddress endereco = null;
protected byte[] texto = new byte[1024];
protected int porta;
public p2pPeerThread(String[] args) throws IOException {
// envia um pacote
texto = args[1].getBytes();
endereco = InetAddress.getByName(args[0]);
porta = Integer.parseInt(args[2]);
// cria um socket datagrama
socket = new DatagramSocket(porta);
}
public void run() {
byte[] texto2 = new byte[1024];
try {
pacote = new DatagramPacket(texto, texto.length, endereco, 8080);
socket.send(pacote);
} catch (IOException e) {
socket.close();
}
while (true) {
try {
// obtem a resposta
pacote = new DatagramPacket(texto2, texto2.length);
socket.setSoTimeout(500);
socket.receive(pacote);
// mostra a resposta
String resposta = new String(pacote.getData(), 0, pacote.getLength());
System.out.println("recebido: " + resposta);
} catch (IOException e) {
System.out.print(".");
}
}
}
}