-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliente.java
More file actions
66 lines (56 loc) · 1.93 KB
/
Cliente.java
File metadata and controls
66 lines (56 loc) · 1.93 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
59
60
61
62
63
64
65
66
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class Cliente {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
String mensagem;
try {
Socket socket = new Socket("localhost", 55573);
// Inicia as threads de entrada e saída
Input inputThread = new Input(socket);
Output outputThread = new Output(socket);
inputThread.start();
outputThread.start();
System.out.println("-=-=-=-=-=-=-=-=-=-=");
System.out.println("Escreva a mensagem: ");
while (true) {
mensagem = scanner.nextLine();
outputThread.enviarMensagem(mensagem);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static class Input extends Thread {
private final DataInputStream entrada;
// Construtor
public Input(Socket socket) throws IOException {
this.entrada = new DataInputStream(socket.getInputStream());
}
public void run() {
while (true) {
String novaMensagem;
try {
novaMensagem = entrada.readUTF();
System.out.println(novaMensagem);
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
}
public static class Output extends Thread {
private final DataOutputStream saida;
// Construtor
public Output(Socket socket) throws IOException {
this.saida = new DataOutputStream(socket.getOutputStream());
}
public void enviarMensagem(String mensagem) throws IOException {
saida.writeUTF(mensagem);
}
}
}