Repository navigation
Expand file tree
/
Copy pathServerConsole.java
More file actions
103 lines (80 loc) · 2.34 KB
/
Copy pathServerConsole.java
File metadata and controls
103 lines (80 loc) · 2.34 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import common.ChatIF;
public class ServerConsole implements ChatIF {
//Class variables *************************************************
/**
* The default port to connect on.
*/
final public static int DEFAULT_PORT = 5555;
//Instance variables **********************************************
/**
* The instance of the client that created this ConsoleChat.
*/
EchoServer server;
//Constructors ****************************************************
/**
* Constructs an instance of the ServerConsole UI.
*
* @param host The host to connect to.
* @param port The port to connect on.
*/
//Constructors ****************************************************
public ServerConsole(int port) {
server = new EchoServer(port);
try {
server.listen();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void accept()
{
// try {
//Enter data using BufferReader
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String cMessage;
try {
while ((cMessage = reader.readLine()) != null) {
server.handleMessageFromServerConsole(cMessage);
this.display(cMessage);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Instance methods ************************************************
/**
* This method waits for input from the console. Once it is
* received, it sends it to all the client
*/
@Override
public void display(String message) {
// TODO Auto-generated method stub
if (message.startsWith("#")) {
return;
}
System.out.println("SERVER MSG>" + message);
}
public static void main(String[] args)
{
int port;
/*
* Similar as
* Part 1 b Client Side but server this time
* it obtains the port number from the command line
* If the port is omitted from the command line, then the default value should still be used
*/
try{
port = Integer.parseInt(args[0]);
}
catch(Exception e){
port = DEFAULT_PORT;
}
ServerConsole server= new ServerConsole(port);
server.accept(); //Wait for console data
}
}