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 pathServerConcorrente.java
More file actions
86 lines (67 loc) · 2.87 KB
/
ServerConcorrente.java
File metadata and controls
86 lines (67 loc) · 2.87 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// SERVER CONCORRENTE TCP
import java.util.*;
import java.io.*;
import java.net.*;
public class ServerConcorrente{
public static void main(String[] args){
final int PORT = 3000;
final ServerSocket server;
final List<Socket> clients = new ArrayList<>();
byte[] buffer = new byte[1000];
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;
}
// ripeti all'infinito le due fasi: accettazione client e dialogare coi client
while(true) {
// accettazione client
System.out.println("--- Fase di connessione");
try {
server.setSoTimeout(2000); // timeout della fase
while (true) {
Socket client = server.accept();
clients.add(client);
System.out.println("Accettato client (" + client.getInetAddress() + " porta " + client.getPort() + ")");
}
} catch(SocketTimeoutException e) {
} catch(Exception e) {
System.out.println("Errore accettazione client, ignoro");
}
// dialogare coi client (facendo timeslicing)
System.out.println("--- Fase di dialogo con client");
Iterator<Socket> iter = clients.iterator();
while (iter.hasNext()) {
Socket client;
// verifica client ancora vivo
try {
client = iter.next();
client.getInetAddress();
} catch(Exception e) {
System.out.println("Errore! Rimuovo client");
iter.remove();
continue;
}
System.out.println("--- Client (" + client.getInetAddress() + " porta " + client.getLocalPort() + ")");
try {
client.setSoTimeout(3); // timeout fase
// dialogo con client
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(SocketTimeoutException e) {
System.out.println("--- Fine tempo, cambio Client");
} catch(Exception e) {
iter.remove();
System.out.println("Errore! Rimuovo client");
continue;
}
}
}
}
}