-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
85 lines (66 loc) · 2.69 KB
/
Server.java
File metadata and controls
85 lines (66 loc) · 2.69 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
// Usage:
// java Server
//
// There is no provision for ending the server gracefully. It will
// end if (and only if) something exceptional happens.
import java.net.*;
import java.io.*;
public class Server {
public static void main(String [] args) {
if (args.length != 1) {
System.err.println("Usage: java Server port");
System.exit(1); // Give up.
}
// Initialize information:
String port = args[0];
// This will be shared by the server threads:
ClientTable clientTable = new ClientTable();
// scoreboardTable
ScoreboardTable scoreboardTable = new ScoreboardTable();
// Open a server socket:
ServerSocket serverSocket = null;
// We must try because it may fail with a checked exception:
try {
//serverSocket = new ServerSocket(Port.number);
serverSocket = new ServerSocket(Integer.parseInt(port));
}
catch (IOException e) {
System.err.println("Couldn't listen on port " + Port.number);
System.exit(1); // Give up.
}
// Good. We succeeded. But we must try again for the same reason:
try {
// We loop for ever, as servers usually do:
while (true) {
// Listen to the socket, accepting connections from new clients:
Socket socket = serverSocket.accept();
// This is so that we can use readLine():
BufferedReader fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// We ask the client what its name is:
String clientName = fromClient.readLine();
// For debugging:
// Repeating users - if a user with the same name enters the game he receives a name with added "1" in the back
if(clientTable.getQueue(clientName) == null){
clientTable.add(clientName);
scoreboardTable.add(clientName);
}else{
clientName = clientName + "1";
clientTable.add(clientName);
scoreboardTable.add(clientName);
}
System.out.println(clientName + " connected");
// We create and start a new thread to read from the client:
(new ServerReceiver(clientName, fromClient, clientTable, scoreboardTable)).start();
// We create and start a new thread to write to the client:
PrintStream toClient = new PrintStream(socket.getOutputStream());
(new ServerSender(clientTable.getQueue(clientName), toClient, clientTable, scoreboardTable)).start();
}
}
catch (IOException e) {
// Lazy approach:
System.err.println("IO error " + e.getMessage());
// A more sophisticated approach could try to establish a new
// connection. But this is beyond this simple exercise.
}
}
}