-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java~
64 lines (44 loc) · 1.33 KB
/
Server.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
// server for testing what servers are
import java.util.*;
import java.net.*;
import java.io.*;
public class Server {
private static Object lock; //for synchronization
private static LinkedList<AccountExec> accounts;
// *an "account" is a server-side thread for managing a connected client
private static Game game;
public static void main(String[] args) {
lock=new Object();
accounts=new LinkedList<AccountExec>();
game=new Game();
try {
ServerSocket serverSocket=new ServerSocket(2000);
System.out.println("Starting server...");
while(true) {
//accept new clients:
Socket s=serverSocket.accept();
synchronized(lock) { //neccessary?
AccountExec exec=new AccountExec(s,accounts.size());
game.addPlayer(exec.getPlayer());
accounts.add(exec);
(new Thread(exec)).start();
System.out.println("New account: "+exec);
}
}
} catch(IOException e) {
System.err.println("error: "+e);
}
} //main
public static void submitAction(GameAction a) {
synchronized(lock) {
game.submitAction(a);
}
} //submitAction
public static void removeAccount(AccountExec accnt) {
synchronized(lock) {
accounts.remove(accnt);
game.removePlayer(accnt.getPlayer());
System.out.println("Removed: "+accnt);
}
} //removeAccount
} //class