-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerConsole.java
70 lines (46 loc) · 1.52 KB
/
ServerConsole.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
67
68
69
70
import java.io.*;
import serveur.EchoServer;
import common.*;
public class ServerConsole implements ChatIF {
//Class variables *************************************************
final public static int DEFAULT_PORT = 5555;
//Instance variables **********************************************
EchoServer server;
//Constructors ****************************************************
public ServerConsole(int port) {
server = new EchoServer(port, this);
}
//Instance methods ************************************************
public void accept() {
try {
BufferedReader fromConsole =
new BufferedReader(new InputStreamReader(System.in));
String message;
while (true) {
message = fromConsole.readLine();
this.server.handleMessageFromServerUI(message);
}
}
catch (Exception ex) {
System.out.println(
"Unexpected error while reading from console!");
}
}
public void display(String message) {
System.out.println(message);
}
//Class methods ***************************************************
public static void main(String[] args) {
int port = 0; //Port to listen on
try {
port = Integer.parseInt(args[0]); //Get port from command line
}
catch(Throwable t) {
port = DEFAULT_PORT; //Set port to 5555
}
ServerConsole sv = new ServerConsole(port);
System.out.println("\nStarting of the server\n");
sv.accept(); //Wait for console data
}
}
//End of ServerConsole class