-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathServer.java
52 lines (40 loc) · 1.05 KB
/
Server.java
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
/*
*
* made by:
* Confalonieri Riccardo
* riccardoconfalonieri98@gmail.com
* github.com/rconfa
*
*/
package server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
class Server {
public static void main(String[] args) throws IOException {
GestioneParcheggi gp = new GestioneParcheggi();
gp.init();
int serverPort = 8080;
// Server in ascolto sulla porta 8080
ServerSocket serverSocket = new ServerSocket(serverPort);
// loop infinito per accettare le richieste
while (true) {
Socket socket = null;
try {
//sincronizzo l'accept perchè non è threadSafe
synchronized(serverSocket) {
// ricevo la richiesta di un client
socket = serverSocket.accept();
}
System.out.println("Nuovo client connesso: " + socket);
// Creazione del nuovo oggetto thread
Thread t = new ClientHandler(socket, gp);
t.start();
} catch (Exception e) {
socket.close();
e.printStackTrace();
break;
}
}
}
}