This repository was archived by the owner on Oct 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
48 lines (35 loc) · 1.45 KB
/
Client.java
File metadata and controls
48 lines (35 loc) · 1.45 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
// CLIENT TCP
import java.io.*;
import java.util.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
final Socket client = new Socket();
final Scanner scanner = new Scanner(System.in);
byte[] buffer = new byte[1000];
// connessione al server
try {
System.out.print(">>> Indirizzo server: ");
InetAddress server = InetAddress.getByName(scanner.nextLine());
System.out.print(">>> Porta server: ");
int port = Integer.parseInt(scanner.nextLine());
client.connect(new InetSocketAddress(server, port));
System.out.println("Connesso con successo al server");
} catch (NumberFormatException | IOException e) {
System.out.println("Errore connessione al server");
}
// scambio di messaggi (senza logica di uscita, aggiungerla in base alla storiella)
while (true) {
try {
System.out.print(">>> Messaggio da inviare: ");
String msg = scanner.nextLine();
client.getOutputStream().write(msg.getBytes());
int len = client.getInputStream().read(buffer);
String res = new String(buffer, 0, len);
System.out.println("Risposta del server: " + res);
} catch (IOException e) {
System.out.println("Errore nello scambio di messaggi");
}
}
}
}