forked from husseinalosman/simpleChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerConsole.java
More file actions
80 lines (63 loc) · 1.54 KB
/
Copy pathServerConsole.java
File metadata and controls
80 lines (63 loc) · 1.54 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
import java.io.*;
import java.util.Scanner;
import client.*;
import common.ChatIF;
public class ServerConsole implements ChatIF {
final public static int DEFAULT_PORT = 5555;
EchoServer server;
Scanner fromConsole;
public ServerConsole(int port){
try{
server= new EchoServer(port);
}
catch(Exception exception){
System.out.println("Error: Can't setup server!"+ " Terminating server.");
System.exit(1);
}
fromConsole = new Scanner(System.in);
}
public void accept() {
try {
String message;
while(true) {
message = fromConsole.nextLine();
server.handleMessageFromServer(message);
}
}
catch (Exception ex){
System.out.println("Unexpected error while reading from console!");
}
}
@Override
public void display(String message) {
// TODO Auto-generated method stub
System.out.println("> " + message);
}
/**
* This method is responsible for the creation of
* the server instance (there is no UI in this phase).
*
* @param args[0] The port number to listen on. Defaults to 5555
* if no argument is entered.
*/
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
}
EchoServer sv = new EchoServer(port);
try
{
sv.listen(); //Start listening for connections
}
catch (Exception ex)
{
System.out.println("ERROR - Could not listen for clients!");
}
}
}