-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
66 lines (62 loc) · 1.91 KB
/
Main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.net.*;
import java.io.*;
public class Main {
private ServerSocket serverSocket;
private Socket socket;
public Main(int port) {
try {
serverSocket = new ServerSocket(port, 1);
}
// creation du serveur
catch (IOException e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
// erreur de création
}
public static void main(String[] args) {
int port;
try {
port = Integer.parseInt(args[0]);
}
catch (Exception e) {
port = 0;
} // donc valeur au hasard
Main ct = new Main(port);
System.out.println(port);
ct.clientMgr();
}
public void clientMgr() {
while (true) {
// écoute
try {
Socket socket = serverSocket.accept();
Thread inputThread = new InputThread(socket.getInputStream());
inputThread.start();
// thread pour lecture
Thread outputThread = new OutputThread(socket.getOutputStream());
outputThread.start();
// thread pour écriture
try {
inputThread.join();
outputThread.join();
} //attente de fin R/W
catch (InterruptedException e) {
System.out.println(e.getMessage());
}
// interruption de thread
}catch (IOException e) {
System.out.println(e);
System.out.println(e.getMessage());
}
finally {
try {
if (socket != null)
socket.close();
}catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
}