-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreads.java
More file actions
58 lines (46 loc) · 1.66 KB
/
Threads.java
File metadata and controls
58 lines (46 loc) · 1.66 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
49
50
51
52
53
54
55
56
57
58
import java.io.*;
import java.net.Socket;
public class Threads extends Thread {
DataInputStream in;
DataOutputStream saida;
private final Socket socket;
// meu Construtor Criando os objetos stream de entrada 'in' e saida 'saida'
public Threads(Socket s) {
this.socket = s;
try {
in = new DataInputStream(socket.getInputStream());
saida = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
// Enviando a mensagem de fato para o outro cliente
/* public void outroClient(String mensagem){
try {
saida.writeUTF(mensagem);
} catch (IOException e) {
e.printStackTrace();
}
}*/
// pegando cada cliente conectado e passando a mensagem para o metodo outroClient do cliente
public void ConectandoClients(String mensagem) throws IOException {
for(Threads client: Servidor.getClients()){
if(client != this){ // se o cliente for diferente do meu proprio cliente
// client.outroClient(mensagem);
client.saida.writeUTF(mensagem); // envia a mensagem para o outro cliente sem precisar do metodo outroClient
}
}
}
// Metodo run da classe thread
public void run() {
while (true) {
try {
String mensagem = in.readUTF();
//System.out.println("Voce: " + mensagem);
ConectandoClients(mensagem);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}