-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPClient.java
More file actions
41 lines (40 loc) · 1.46 KB
/
Copy pathTCPClient.java
File metadata and controls
41 lines (40 loc) · 1.46 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
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class TCPClient {
public static void main(String[] args) {
Socket s = null;
try {
s = new Socket("localhost", 6789);
DataInputStream ent = new DataInputStream(s.getInputStream());
DataOutputStream sai = new DataOutputStream(s.getOutputStream());
sai.writeUTF("TESTE");
System.out.print("Digite o nome do arquivo: ");
Scanner in = new Scanner(System.in);
sai.writeUTF(in.nextLine());
in.close();
String recebido = ent.readUTF();
while (recebido != null) {
System.out.println(recebido);
recebido = ent.readUTF();
}
} catch (UnknownHostException e) {
System.out.println("Servidor desconhecido: " + e.getMessage());
} catch (EOFException e) {
System.out.println("--- FIM DA TRANSFERENCIA ---");
} catch (IOException e) {
System.out.println("E/S: " + e.getMessage());
} finally {
if (s!=null)
try {
s.close();
} catch (IOException e){
System.out.println("Encerramento do socket falhou: " + e.getMessage());
}
}
}
}