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 pathServerIterativo.java
More file actions
45 lines (34 loc) · 1.46 KB
/
ServerIterativo.java
File metadata and controls
45 lines (34 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
42
43
44
45
// SERVER ITERATIVO TCP
import java.io.*;
import java.net.*;
public class ServerIterativo {
public static void main(String[] args) {
final int PORT = 3000;
final ServerSocket server;
byte[] buffer = new byte[1000];
// creazione server
try {
server = new ServerSocket(PORT);
System.out.println("Creato server: " + server.getInetAddress() + " porta " + server.getLocalPort());
} catch (IOException e) {
System.out.println("Errore creazione server");
return;
}
// scambio di messaggi (senza logica di cambio client se non errore, aggiungerla in base alla storiella)
while (true) {
Socket client;
try {
client = server.accept();
System.out.println("Accettato client (" + client.getInetAddress() + " porta " + client.getPort() + ")");
while (true) {
int len = client.getInputStream().read(buffer);
String msg = new String(buffer, 0, len);
System.out.println("Ricevuto da (" + client.getInetAddress() + " porta " + client.getPort() + "):\n" + msg);
client.getOutputStream().write("ACK".getBytes());
}
} catch (StringIndexOutOfBoundsException | IOException e) {
System.out.println("Errore connessione, chiudo e aspetto prossimo client");
}
}
}
}